JavaScript使用addEventListener添加事件监听用法实例
本文实例讲述了JavaScript使用addEventListener添加事件监听用法。分享给大家供大家参考。具体实现方法如下:
<!DOCTYPEhtml> <htmllang="en"> <head> <title>Thistextisthetitleofthedocument</title> <script> functionshowalert(){ alert('youclickedme!'); } functionclickme(){ varpelement=document.getElementsByTagName('p')[0]; pelement.addEventListener('click',showalert,false); } window.addEventListener('load',clickme,false); </script> </head> <body> <divid="main"> <p>ClickMe</p> <p>YouCan'tClickMe</p> </div> </body> </html>
希望本文所述对大家的javascript程序设计有所帮助。