Addeventlistener and attachevent Summary

Source: Internet
Author: User

The DOM event stream consists of three phases: Event capture, target, and event bubbles.

The idea of event capturing is that an event should be received at an earlier time on a node that is never too specific, and the most specific node should finally receive the event.

Event bubbling events are accepted by the most specific elements at the beginning, and then spread to unspecified nodes step by step.

The idea of weak coupling is to separate HTML from JavaScript.

First Dom method:

Bind event: ELEM. type = FN () {} // element name. event type = function name () {} event type such as onclick

Remove event: ELEM. type = NULL;

Dom2 adopts the following methods:

Binding event: addeventlistener (ELEM, type, bool) // event type, such as click

Remove event: removeeventlisnter (ELEM, type, bool)

IE-DOM2 with the way:

Binding event: attachevent (ELEM, type) // event type, for example, onclick
Remove event: detachevent (ELEM, type)

Note:

In the standard DOM, the event handler will line this === a specific element in the scope of the element to which it belongs.
You can also use event.tar get to point to a specific element.

The event handler in the IE-DOM1 runs this === specific element within the scope of its element

The event handler in the IE-DOM2 runs this = window in the global scope

To point to the specific element event. srcelemnt

VaR eventutil = {
Addhandler: function (element, type, Handler ){
If (element. addeventlistener ){
Element. addeventlisener (type, handler, false );
}
Else if (element. attachevent ){
Element. attachevent ("On" + type, Handler );
}
Else {
Element ["on" + type] = handler;
}
},

Removehandler: function (element, type, Handler ){
If (element. removeeventlistener ){
Element. removeeventlistener (type, handler, false );
}

Else if (element. detachevent ){
Element. detachevent ("On" + type, Handler );
}
Else {
Element ["on" + type] = NULL;
}
}
}

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.