请问是哪里出错呢?感觉没错啊。 <!DOCTYPE html> <html> <head> <title></title> </head> <body> <input type="button" value="This is a code" id="one"> <input type="button" value="This is a code" id="two" onClick="a()"> <input type="button" value="This is a code" id="three"> <script type="text/javascript"> var one = document.getElementById("one"); one.onClick=function () { alert('kekeke'); } function a() { alert('hahaha'); } var three = document.getElementById('three'); three.addEventListener('click',a,false); var four = document.getElementById("four"); four.attachEvent('onclick',a); </script> </body> </html>
第一个:one.onclick;onclick字母小写;
第四个:你有input按钮吗?
还有测试addEventListener和attachEvent要分开浏览器测试,先注释掉一个