Test <script> <br>function test() <br>{ <br>alert("OK"); <br>} <br>var x=document.getElementById("a"); <br>x.onclick=test;/ /Note there is no () <br></script>
test <script> <br>function test() <br>{ <br>alert("OK"); <br>} <br>var x=document.getElementById("a"); <br>x.addEventListener("click",test,false ; Indicates whether event bubbling (true) or event capturing (false) involves the order of event occurrence from bottom to top or top to bottom. <br>The method to remove an event is removeEventListener(), the parameters are the same as addEventListener(). <br>This mode will report an error in IE browser, but execute normally in chrome. </div>
<br>Fourth (Microsoft model): <br><br><br><br><br>Copy code<div class="codetitle">
<span><a style="CURSOR: pointer" data="28228" class="copybut" id="copybut28228" onclick="doCopy('code28228')"> The code is as follows:<u></u> </a><a href="#" id="a">test</a> </span><script> </div>function test() <div class="codebody" id="code28228">{ <br>alert("OK") ; <br>} <br>var x=document.getElementById("a"); <br>x.attachEvent("onclick",test); <br></script>
Among the two parameters of attachEvent(), the first one indicates the event type, please note that it is on, and the second one specifies the method.
The method to remove an event is detachEvent(), where the parameters are the same as attachEvent().