您现在的位置: 万盛学电脑网 >> 程序编程 >> 脚本专题 >> javascript >> 正文

JavaScript使用addEventListener添加事件监听用法实例

作者:佚名    责任编辑:admin    更新时间:2022-06-22

   本文实例讲述了JavaScript使用addEventListener添加事件监听用法。分享给大家供大家参考。具体实现方法如下:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 <!DOCTYPE html> <html lang="en"> <head> <title>This text is the title of the document</title> <script> function showalert(){ alert('you clicked me!'); } function clickme(){ var pelement=document.getElementsByTagName('p')[0]; pelement.addEventListener('click', showalert, false); } window.addEventListener('load', clickme, false); </script> </head> <body> <div id="main"> <p>Click Me</p> <p>You Can't Click Me</p> </div> </body> </html>

  希望本文所述对大家的javascript程序设计有所帮助。