Home > Web Front-end > JS Tutorial > Detailed explanation of event flow and event handlers in JavaScript (graphic tutorial)

Detailed explanation of event flow and event handlers in JavaScript (graphic tutorial)

亚连
Release: 2018-05-21 13:47:26
Original
1599 people have browsed it

The event stream refers to the order in which events are received from the page, while the event handler handles the response to the event. Next, we will explain the event stream and event handler in JavaScript in detail.

Events Stream: There are two types. IE's is an event bubbling stream. The event is received from the most specific element at the beginning and propagated upwards to less specific nodes (Element -> Document). The opposite is Netscape's event capture stream.

DOM2-level events stipulate that the event flow includes three stages: event capture stage, target stage and event bubbling stage.

In most cases, event handlers are added to the bubbling phase of the event flow. An example of EventUtil:

var EventUtil = {
  addHandler: function(element, type, handler){
    if(element.addEventListener){
      element.addEventListener(type, handler, false);
    }else if(element.attachEvent){
      element.attachEvent('on' + type, handler); // IE8
    }else{
      element['on' + type] = handler;
    }
  },
  removeHandler: function(){...}
}
Copy after login

Let’s look at it in detail:

DOM0-level event handlerThe traditional way to specify an event handler through Javascript is to assign a function Give an event handler attribute.
Each element has its own event handler attributes, which are usually all lowercase, such as onclick. By setting the value of this property to a function, you can specify an event handler.

var btn = document.getElementById('myBtn');
// 添加事件处理程序
btn.onclick = function () {
  alert( this );//为DOM元素btn
};
// 移除事件处理程序
btn.onclick = null;
Copy after login

Advantages: 1. Simple 2. Cross-browser advantages
Disadvantages: Event handlers are not specified before the code is run, so these codes are located behind the buttons in the page, and there is a possibility of There is no response no matter how you click for a period of time, and the user experience becomes worse.

DOM2-level event handler defines two methods for handling the operations of specifying and removing event handlers: addEventListener() and removeEventListener(). Three parameters, 1. The name of the event to be processed. 2. A function as an event handler 3. A Boolean value. The last Boolean value is true, which means the event handler is called during the capture phase, and false, which means the event handler is called during the bubbling phase.

// 添加多个事件处理程序
var btn = document.getElementById('myBtn');
btn.addEventListener('click',function (){
  alert( this );// 为DOM元素btn
},false );
btn.addEventListener('click',function () {
  alert('Hello World');
},false);

// 移除事件处理程序
btn.removeEventListener('click',function () {
  // 匿名函数无法被移除,移除失败
},false);
  // 改写
  var handler = function () {
  alert(this.id);
  };
  btn.addEventListener('click',handler,false);
  // 再次移除事件处理程序
  btn.removeEventListener('click',handler,false);// 移除成功
Copy after login

These two event handlers will fire in the order in which they are added. In most cases, event handlers are added to the bubbling phase of the event flow, so as to maximize compatibility with various versions of browsers.

Advantages: One element can add multiple event handlers
Disadvantages: IE8 and below browsers do not support DOM2-level event handlers. (Including IE8)

IE event handler defines two methods, similar to the above: attachEvent(), detachEvent(). Both methods receive the same two parameters: event handler name and event handler function. Since IE8 and earlier browsers only support event bubbling, event handlers added through detachEvent() will be added to the bubbling phase.

var btn = document.getElementById('myBtn');
btn.attachEvent('onclick', function(){
  alert( this );// window
});
btn.attachEvent('onclick', funciton(){
  alert("HELLO, WORLD");
});
Copy after login

When the button is clicked, the firing order of these two event handlers is exactly the opposite of the above. Instead of triggering the event handlers in the order they were added, just the opposite.

Advantages: Multiple event handlers can be added to one element
Disadvantages: Only supports IE.

Cross-browser event handler

eg:

var EventUtil = {
  addHandler : function ( ele, type, handler ) {
    if ( ele.addEventListener ) {
      ele.addEventListener( type, handler, false );
    } else if ( ele.attachEvent ) {
      ele.attachEvent( 'on' + type, handler );
    } else {
      ele['on' + type] = handler
    }
  },
  removeHandler: function ( ele, type, handler ) {
    if ( ele.removeEventListener ) {
      ele.removeEventListener( type, handler, false );
    } else if ( ele.detachEvent ) {
      ele.detachEvent( 'on' + type, handler );
    } else {
      ele[ 'on' + type ] = null;
    }
  }
}
Copy after login

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Basic JavaScript tips (picture and text tutorials, detailed answers for you)

Traversing the values ​​​​in the EL expression List collection in javascript

Detailed analysis and answers to the principle of JavaScript operation

The above is the detailed content of Detailed explanation of event flow and event handlers in JavaScript (graphic tutorial). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template