JavaScript debugging
要怎麼解決 Uncaught TypeError: Cannot set property ‘onClick’ of null ?
HTML DOM Document addEventListener()
document.getElementById("#register button").onclick=function(){ alert('222'); }
//方法一:
window.onload = function () { document.getElementById("register").onclick=function(){ alert('222'); } }
//方法二:(支援各大瀏覽器)
if(window.addEventListener){ window.addEventListener("load",alert1,false); window.addEventListener("load",alert2,false); } else{ window.attachEvent("onload",alert1); window.attachEvent("onload",alert2); } function alert1(){ document.getElementById("register").onclick=function () { alert('測試1'); }; } function alert2(){ document.getElementById("register").onclick=function () { alert('測試2); }; }
https://stackoverflow.com/questions/16222895/uncaught-typeerror-cannot-set-property-onclick-of-null-onclick-not-working
https://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element
留言