Home > Web Front-end > JS Tutorial > 4 ways to register and remove events in javascript_javascript skills

4 ways to register and remove events in javascript_javascript skills

WBOY
Release: 2016-05-16 17:40:11
Original
1195 people have browsed it

There are many ways to register events for some elements in html
The first one:

Copy the code The code is as follows:

<script> <br>function test() <br>{ <br>alert("OK"); <br>} <br></script>
< a href="#" onclick="test()">Test

Second type:
Copy Code The code is as follows:

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>

Third type (W3C model):
Copy code The code is as follows:

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().
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template