JavascriptattachEvent binds multiple event execution sequence question _ javascript skills

Source: Internet
Author: User
The execution sequence is irregular, but the execution sequence is the same. If it is random, the execution sequence should be different each time, this is the common binding event that I want to describe. These events are directly bound to page elements, such

This method is separated and written in js Code, such as document. getElementById ('wrapp '). onclick = function () {a ();}. If you need to bind multiple methods, write them together, as shown in document. getElementById ('wrapp '). onclick = function () {a (); B ();} or

. However, we often write a separate bind (el, name, fn) binding method as follows.

The Code is as follows:


Function bind (el, name, fn) {// bind event
Return el. addEventListener? El. addEventListener (name, fn, false): el. attachEvent ('on' + name, fn );
}


In this way, multiple click events are bound to a dom object, as shown in figure

The Code is as follows:


Bind (document. getElementById ('wrapp'), 'click', );
Bind (document. getElementById ('wrapp'), 'click', B );


In ie6, there will be execution sequence problems (ie9 is not clear below, there is no environment test, hope friends with the environment to help test), while other browsers ff, chrome, in safari, it is executed in order. The query results show that ie8 will reverse the execution order. ie6 and ie7 will be executed randomly. I really don't understand how ie will be executed randomly? Is there a specific purpose? If you know the purpose of doing so, you can also tell me that, after testing, ie8 is indeed reversing the execution order, while ie6 and 7 are executing irregularly. I understand that random execution is a different sequence for each execution. However, as long as the order is fixed during writing, the order for execution is also fixed (although there is no rule, maybe I did not find it ).

But the $. bind (type, data, fn) method of jQuery does not have this problem. After looking for the original jQuery code, I found that it is also used.

The Code is as follows:


If (elem. addEventListener)
Elem. addEventListener (type, handle, false );
Else if (elem. attachEvent)
Elem. attachEvent ("on" + type, handle );


This method binds events, but before that, it will determine whether the jquery object has handlers of the same type, if yes, the handle of the object is merged into the handlers to form a method equivalent to function c () {a (); B ();}, in this way, the execution sequence is not disordered when multiple methods are bound in ie.

The following is a reply from a netizen:

Will IE6 7 be executed randomly? During my last test, it seems that the order is the opposite. I did not perform this statement randomly.
I specifically tested it.

The Code is as follows:


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.