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>