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;
}
}
}