There are two types of event bindings: Traditional event binding (inline model, script model), and modern event binding
(DOM2-level model). Modern event binding provides more powerful and convenient functionality on traditional bindings.
A Issues with traditional event bindings
Traditional event binding has inline model and script model, inline model we do not discuss, rarely use. First Look
Script model, the script model assigns a function to an event handler function.
- var box = document.getElementById (' box '); Get element
- Box.onclick = function () {//Element click Trigger Event
- Alert (' Lee ');
- };
Issue one: An event handler function triggers two events
- Window.onload = function () {//First set of program project or first JS file
- Alert (' Lee ');
- };
- Window.onload = function () {//second set of program project or second JS file
- Alert (' Mr.Lee ');
- };
When two sets of programs or two JS files are executed simultaneously, the latter one will completely overwrite the previous one. Lead
The window.onload in front has completely failed.
To solve the coverage problem, we can solve it this way:
- Window.onload = function () {//The first event to be executed will be overwritten
- Alert (' Lee ');
- };
- if (typeof window.onload = = ' function ') {//Before judging if there is window.onload
- var saved = null; Create a Save
- saved = window.onload; Save the previous window.onload.
- }
- Window.onload = function () {//FINAL one to execute event
- if (saved) saved (); Perform a previous event
- Alert (' Mr.Lee '); Code to execute this event
- };
Issue two: Event switcher
- Box.onclick = Toblue; First execution Boblue ()
- function tored () {
- This.classname = ' red ';
- This.onclick = Toblue; Execute Toblue () for the third time, then switch back and forth
- }
- function Toblue () {
- This.classname = ' Blue ';
- This.onclick = tored; Second Execution tored ()
- }
There are some problems with this switch when it expands:
1. If an execution function is added, it will be overwritten
- Box.onclick = Toalert; The added function
- Box.onclick = Toblue; Toalert is covered.
2. If the overwrite problem is resolved, it must include simultaneous execution, but a new problem
- Box.onclick = function () {//included in, but less readable
- Toalert (); The first time will not be overwritten, but the second time is overwritten
- Toblue.call (this); You must also pass this to the switcher.
- };
Three issues covered: coverage, readability, this delivery problem. Let's create a custom thing
To solve the above three problems.
- function addevent (obj, type, fn) {//Replace traditional event handler
- var saved = null; Save event handlers for each trigger
- if (typeof obj[' on ' + type] = = ' function ') {//Judging is not an event
- Saved = obj[' on ' + type]; If there is, save it.
- }
- obj[' on ' + type] = function () {///Then Execute
- if (saved) saved (); Perform the previous
- Fn.call (this); Execute the function and pass this to the past
- };
- }
- addevent (window, ' Load ', function () {///execute to
- Alert (' Lee ');
- });
- addevent (window, ' Load ', function () {///execute to
- Alert (' Mr.Lee ');
- });
PS: The above-written custom event handler, there is another problem is not handled, that is, two of the same function name
function is incorrectly registered for two or more times, you should block out the excess. Then we need to move the event handler into
Line traversal, if a function name with the same name is not added. (We don't do it here.)
- addevent (window, ' Load ', init); Register for the first time
- addevent (window, ' Load ', init); Register the second time, should ignore
- function init () {
- Alert (' Lee ');
- }
Use the custom event function to register to the switcher to see the effect:
- addevent (window, ' Load ', function () {
- var box = document.getElementById (' box ');
- addevent (box, ' click ', Toblue);
- });
- function tored () {
- This.classname = ' red ';
- Addevent (This, ' click ', Toblue);
- }
- function Toblue () {
- This.classname = ' Blue ';
- Addevent (This, ' click ', tored);
- }
PS: When you click a lot of many times after switching, the browser dies directly, or an error pops up: Too much
Recursion (too many recursion). The main reason is that every time the event is switched, it is saved and no useless
removed, resulting in the accumulation of more, the last card to die.
- function removeevent (obj, type) {
- if (obj[' on '] + type) obj[' on ' + type] = NULL; Delete event handler function
- }
The above delete event handler is just a one-size-fits-all delete, which resolves the card and too many recursive
Problem. But the other event handlers are also deleted, resulting in the final result not getting what they want. If you want to
To delete only the event handlers in the specified function, you need to traverse and find. (We don't do it here.)
Two Event handling function
The "DOM2 level event" defines two methods for adding events and deleting event handlers:
AddEventListener () and RemoveEventListener (). Both methods are included in all DOM nodes, and it
We all accept 3 parameters; The event name, function, bubbling, or captured Boolean value (true means capture, false for bubbling).
- Window.addeventlistener (' Load ', function () {
- Alert (' Lee ');
- }, False);
- Window.addeventlistener (' Load ', function () {
- Alert (' Mr.Lee ');
- }, False);
The ps:w3c of modern event binding is better than our custom: 1. No customization is required; 2. Can shield the phase
the same function; 3. You can set bubbles and captures.
- Window.addeventlistener (' Load ', init, false); For the first time.
- Window.addeventlistener (' Load ', init, false); It's been blocked for the second time.
- function init () {
- Alert (' Lee ');
- }
Event switcher
- window.addeventlistener (' Load ', function () {
- var box = document.getElementById (' box ');
- box.addeventlistener (' click ', function () { Will not be mistakenly deleted
- alert (' Lee ');
- }, false);
- box.addeventlistener (' click ', toblue, false); //Introduction switch is not too much recursive card dead
- }, false);
- function Tored () {
- this.classname = ' red ';
- this.removeeventlistener (' click ', tored, false);
- this.addeventlistener (' click ', toblue, false);
- }
- function toblue () {
- this.classname = ' Blue ';
- this.removeeventlistener (' click ', toblue, false);
- this.addeventlistener (' click ', tored, false);
- }
Setting the bubbling and capturing stages
In the previous chapter we learned about event bubbling, which is triggered from inside to outside. We can also use the event object to block
Bubbling at a certain stage. Then the modern event bindings can set bubbles and captures.
- Document.addeventlistener (' click ', function () {
- Alert (' Document ');
- }, True); Sets the Boolean value to True to capture the
- Box.addeventlistener (' click ', function () {
- Alert (' Lee ');
- }, True); Set the Boolean value to False to bubble
Three IE event handler function
IE implements two methods similar to the one in the DOM: Attachevent () and DetachEvent (). These two methods accept
Same parameter: event name and function.
In the use of these two sets of functions, the first to say the difference: 1. IE does not support capture, only supports bubbling; 2. IE add
The event cannot mask repeated functions; 3. The this in IE points to a window instead of a DOM object. 4. In traditional matters
, IE is not acceptable to the event object, but using Attchevent () can, but there are some differences.
- Window.attachevent (' onload ', function () {
- var box = document.getElementById (' box ');
- Box.attachevent (' onclick ', toblue);
- });
- function tored () {
- var that = window.event.srcElement;
- That.classname = ' red ';
- That.detachevent (' onclick ', tored);
- That.attachevent (' onclick ', toblue);
- }
- function Toblue () {
- var that = window.event.srcElement;
- That.classname = ' Blue ';
- That.detachevent (' onclick ', toblue);
- That.attachevent (' onclick ', tored);
- }
Ps:ie does not support capture, no solution. IE cannot be masked and needs to be expanded individually or custom event handling. IE cannot
Pass this and you can call the past.
- Window.attachevent (' onload ', function () {
- var box = document.getElementById (' box ');
- Box.attachevent (' onclick ', function () {
- Alert (This = = = window); This points to the window
- });
- });
- Window.attachevent (' onload ', function () {
- var box = document.getElementById (' box ');
- Box.attachevent (' onclick ', function () {
- Toblue.call (box); Direct this to the past
- });
- });
- function Tothis () {
- alert (this.tagname);
- }
In traditional bindings, IE is not able to accept the event object as a pass-through, but if you use the
Attachevent () can.
- Box.onclick = function (evt) {
- alert (EVT); Undefined
- }
- Box.attachevent (' onclick ', function (evt) {
- alert (EVT); Object
- alert (Evt.type); Click
- });
- Box.attachevent (' onclick ', function (evt) {
- Alert (evt.srcelement = = = box); True
- Alert (window.event.srcElement = = = box); True
- });
Finally, in order for IE and the Internet Explorer to be compatible with this event switcher, we can write the following method:
- function addevent (obj, type, fn) {//Add event compatible
- if (Obj.addeventlistener) {
- Obj.addeventlistener (type, FN);
- } else if (obj.attachevent) {
- Obj.attachevent (' on ' + type, fn);
- }
- }
- function removeevent (obj, type, fn) {//Remove event compatible
- if (Obj.removeeventlistener) {
- Obj.removeeventlistener (type, FN);
- } else if (obj.detachevent) {
- Obj.detachevent (' on ' + type, fn);
- }
- }
- function Gettarget (evt) {//Get event Target
- if (evt.target) {
- return evt.target;
- } else if (window.event.srcElement) {
- return window.event.srcElement;
- }
- }
PS: Invoke Ignore, IE compatible event, if you want to pass this, change to call can.
The event binding functions in Ps:ie attachevent () and detachevent () may not be used in practice, with several
Cause: 1. IE9 will fully support the event binding function in the universal, 2. The event binding function of IE cannot pass this;3. Ie
The event binding function does not support capture; 4. After the same function is registered, it is not masked; 5. There is a memory leak problem.
As for how to replace it, we will discuss it in the course of future projects.
Four Additional additions to the event object
A property is provided in the Relatedtarget: This property can be used in mouseover and mouseout events
Gets the DOM object from where and from where it was moved.
- Box.onmouseover = function (evt) {//mouse move into box
- alert (evt.relatedtarget); Gets the element object that was recently moved into box
- }//span
- Box.onmouseout = function (evt) {//mouse move out box
- alert (evt.relatedtarget); Gets the element object that moved out of box most recently
- }//span
IE provides two sets of properties for moving in and out: Fromelement and toelement, respectively, corresponding to MouseOver
and Mouseout.
- Box.onmouseover = function (evt) {//mouse move into box
- alert (window.event.fromElement.tagName); Gets the nearest element object span moved into box
- }
- Box.onmouseout = function (evt) {//mouse move into box
- alert (window.event.toElement.tagName); Gets the nearest element object span moved into box
- }
Ps:fromelement and toelement do not have any meaning if they correspond to the opposite mouse events respectively.
All that's left to do is cross-browser compatible:
- function Gettarget (evt) {
- var e = evt | | window.event; Get Event Object
- if (e.srcelement) {//If srcelement is supported, indicates IE
- if (E.type = = ' MouseOver ') {//if it is over
- return e.fromelement; Just use the From
- } else if (E.type = = ' mouseout ') {//if it is out
- return e.toelement; Just use the To
- }
- } else if (e.relatedtarget) {//If Relatedtarget is supported, the
- return e.relatedtarget;
- }
- }
Sometimes we need to block the default behavior of the event, such as: The default behavior of a hyperlink and then jump to the point
The page is set. This prevents the default behavior from blocking the jump, and implements the custom action.
The default behavior of canceling an event is also a non-canonical practice, which is to return false.
- Link.onclick = function () {
- Alert (' Lee ');
- return false; Give a fake directly, you will not jump.
- };
PS: Although return false; can implement this function, but there is a loophole; first: it must be written to the last, which causes
After execution of the middle code, it is possible to perform a return false; second: return false writes to the last
The definition operation is invalidated. So, the best way to do this is to block the default behavior at the top, and you can do it later
Code.
- Link.onclick = function (evt) {
- Evt.preventdefault (); Stop the default behavior and place it wherever you are
- Alert (' Lee ');
- };
- Link.onclick = function (evt) {//ie, blocking default behavior
- Window.event.returnValue = false;
- Alert (' Lee ');
- };
Cross-Browser compatible
- function Predef (evt) {
- var e = evt | | window.event;
- if (E.preventdefault) {
- E.preventdefault ();
- } else {
- E.returnvalue = false;
- }
- }
Context Menu Event: ContextMenu, when we right-click on a webpage, it automatically appears with Windows
Menu. Then we can use the ContextMenu event to modify the menu we specified, but only if you right-click the default
The behavior is cancelled out.
- addevent (window, ' Load ', function () {
- var text = document.getElementById (' text ');
- Addevent (text, ' ContextMenu ', function (evt) {
- var e = evt | | window.event;
- Predef (e);
- var menu = document.getElementById (' menu ');
- Menu.style.left = e.clientx + ' px ';
- Menu.style.top = e.clienty + ' px ';
- menu.style.visibility = ' visible ';
- Addevent (document, ' click ', function () {
- document.getElementById (' MyMenu '). style.visibility = ' hidden ';
- });
- });
- });
Ps:contextmenu events are common, which leads directly to a more stable browser compatibility.
Pre-uninstallation event: Beforeunload, this event can help you leave this page with the appropriate prompt, "away from
"or" return "operation.
- addevent (window, ' beforeunload ', function (evt) {
- Predef (EVT);
- });
Mouse wheel (mousewheel) and dommousescroll to get the mouse up and down the scroll wheel distance.
- Addevent (document, ' MouseWheel ', function (evt) {//non-Firefox
- Alert (GETWD (EVT));
- });
- Addevent (document, ' Dommousescroll ', function (evt) {//Firefox
- Alert (GETWD (EVT));
- });
- function Getwd (evt) {
- var e = evt | | window.event;
- if (E.wheeldelta) {
- return E.wheeldelta;
- } else if (E.detail) {
- Return-evt.detail * 30; Maintain the unity of computation
- }
- }
PS: Through browser detection you can determine that Firefox only executes Dommousescroll.
The domcontentloaded event and ReadyStateChange event, about Dom loading, about the
The contents of the two events are very numerous and complex, and we will first point here and discuss them in detail during the course of the project.
JavaScript event bindings and in-depth