The first type: button.onclick = Function("alert('hello');");
The second type: button.onclick = function(){alert("hello"); };
The third type: button.onclick = myAlert;
function myAlert(){
alert("hello");
}
The fourth type:
This situation is more dynamic and practical, and can also add multiple functions (the order of added events is the order of execution), haha
if(window.addEventListener){ // Mozilla, Netscape, Firefox //element.addEventListener(type,listener,useCapture); button.addEventListener('click', alert('11'), false); button.addEventListener('click', alert('12'), false); //Execution order 11 -> 12 } else { // IE button.attachEvent('onclick', function(){alert('21');}); button.attachEvent('onclick', function(){alert('22');});Execution order 22 -> 21 }Example explanation:
button.onclick = Function ("alert('31');"); button.onclick = Function ("alert('32');"); button.onclick = Function ("alert('33');"); //If you write this way, then only the last method will be executed button.attachEvent("onclick", function(){alert('41');}); button.attachEvent("onclick", function(){alert('42');}); button.attachEvent("onclick", function(){alert('43');}); //If you write this way, all three methods will be executed// Of course, you can also write button.onclick = Function("alert('51');"); button.attachEvent("onclick", function(){alert('52');}); //Remotely remove event detachEvent('onclick' ,func); //Use delete event func under ie func removeEventListener('click' ,func); //Use delete event func under Mozilla, delete event funcThe above four methods (recommended) of JS to modify the onclick action are all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.