Home >Web Front-end >JS Tutorial >A brief analysis of Javascript registration events_javascript skills

A brief analysis of Javascript registration events_javascript skills

WBOY
WBOYOriginal
2016-05-16 19:01:47835browse

First is the most conventional method:


[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]

When one day, we know After JavaScript is separated from the HTML structure, it will be written in a different way:

[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]
When we work longer and longer Finally, sometimes we need to bind multiple same event types to an element: test
[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]
If you follow the above writing method, We can only output the second function.
test

[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]

In a period of time, you I didn't find any errors in this code.
One day, a browser named firefox broke into your field of vision. When we put this code into firefox and executed it,
we found that it did not run properly. There are more and more problems like this, but as a JS programmer, these are all things we must face.
test


[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh it to execute
]

test



[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute it <script> function test(){ alert("test"); } </script>]<script> function test(){ alert("test"); } window.onload = function(){ document.getElementById("para").onclick = test; } </script> <script> function test(){ alert("test"); } function pig(){ alert("pig"); } window.onload = function(){ document.getElementById("para").onclick = test; document.getElementById("para").onclick = pig; } </script>At this point, as a programmer The work is done. <script> function test(){ alert("test"); } function pig(){ alert("pig"); } window.onload = function(){ document.getElementById("para").attachEvent("onclick",test); document.getElementById("para").attachEvent("onclick",pig); } </script>In the middle, we started from the most traditional and basic writing method, then realized the separation of Js and HTML, and then realized the registration of multiple events for the same element. During this period, we discovered the compatibility issue of registered events. Finally, we encapsulate the method of registering events for future use. <script> function test(){ alert("test"); } function pig(){ alert("pig"); } window.onload = function(){ var element = document.getElementById("para"); if(element.addEventListener){ // firefox , w3c element.addEventListener("click",test,false); element.addEventListener("click",pig,false); } else { // ie element.attachEvent("onclick",test); element.attachEvent("onclick",pig); } } </script><script> function test(){ alert("test"); } function pig(){ alert("pig"); } function addListener(element,e,fn){ if(element.addEventListener){ element.addEventListener(e,fn,false); } else { element.attachEvent("on" + e,fn); } } window.onload = function(){ var element = document.getElementById("para"); addListener(element,"click",test); addListener(element,"click",pig); } </script>Okay, this is the end of the article. I hope everyone has gained something. . .
Statement:
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