Common binding events are directly bound to page elements such as <div id= "wrap" onclick= "A ();" ></div>, this method of change is to separate out and write in the JS code such as document.getElementById (' wrap '). onclick = function () {a ();}, If you need to bind more than one method at this point, you can write directly together as document.getElementById (' wrap '). onclick = function () {a (); B ();} or <div id= "wrap" onclick= "A (); B ();" ></div>. However, we often write a separate binding method, bind (EL,NAME,FN), with the following specific code.
Copy Code code as follows:
function bind (EL,NAME,FN) {//Bind event
Return El.addeventlistener?el.addeventlistener (Name,fn,false): El.attachevent (' on ' +name,fn);
}
This allows multiple click events to be bound on a DOM object such as
Copy Code code as follows:
Bind (document.getElementById (' wrap '), ' click ', a);
Bind (document.getElementById (' wrap '), ' click ', b);
The order of execution will appear below the ie6,7,8 (IE9 is temporarily unclear, there is no environmental test, want to have the environment of friends to help test), and other browsers Ff,chrome,safari under the order of execution. Find the data is IE8 will reverse the order of execution, IE6,IE7 is random execution, this I really do not understand how the IE will appear random execution? Do you have a purpose? If there is a friend who knows the purpose of doing so can also tell a sound, after their own test confirmed IE8 really reverse the order of execution and ie6,7 is irregular execution. I understand that random execution should be a different order for every execution, but in practice, the order is fixed when the order is written (though it may be irregular or I do not find the rule).
However, the jquery $.bind (type, data, FN) method does not have this problem, looking for jquery's original code to look at, found inside is also used
Copy Code code as follows:
if (Elem.addeventlistener)
Elem.addeventlistener (type, handle, false);
else if (elem.attachevent)
Elem.attachevent ("On" + type, handle);
The event that this method binds, but before this determines whether the jquery object already has the same type of handlers, if there are not repeated bindings instead of merging the handle of the object into handlers is a method equivalent to this function C () {A () b (); The sequential indentation method implements the order of execution in which there is no binding of multiple methods under IE.
The following is a netizen's reply:
IE6 7 will execute randomly? The last time I tested it looked like it was in reverse order, and there was no random execution of that.
I went to the test.
Copy Code code as follows:
<script type= "Text/javascript" >
var bind = function (E,T,FN) {
if (E.addeventlistener) {
E.addeventlistener (T, FN, false);
}else if (e.attachevent) {
E.attachevent (' on ' + t, fn);
}
}
</script>
<a href= "#" id= "Test1" >test</a>
<script type= "Text/javascript" >
var e = document.getElementById (' test1 ');
Bind (E, ' click ', function () {alert (1)});
Bind (E, ' click ', function () {alert (2)});
Bind (E, ' click ', function () {alert (3)});
Bind (E, ' click ', function () {alert (4)});
Bind (E, ' click ', function () {alert (5)});
</script>
There is no random one said IE6. I don't know how you test it.
The author's answer:
I take your code under the IE6,IE7 test is also irregular execution order, here is the source of information http://www.w3help.org/zh-cn/causes/SD9011, the above so-called random execution I also feel defective, as I Bovenri said, The order of execution is irregular, but the order of execution is the same, and if it is random then the order of execution should be different, which is what I want to explain.