Home  >  Article  >  Web Front-end  >  [JavaScript] Two completely opposite event flows: event bubbling and event capturing

[JavaScript] Two completely opposite event flows: event bubbling and event capturing

php是最好的语言
php是最好的语言Original
2018-08-02 09:13:262052browse

What is an event?

Events are specific moments of interaction that occur between a document and a browser window.

What is event flow:

Event flow describes the order in which events are received from the page, but interestingly, Microsoft (IE) and Netscape (Netscape) ) The development team actually proposed two completely opposite event stream concepts. IE's event stream is event bubbling stream (event bubbling), while Netscape's event stream is event capturing stream (event capturing).

The first type: event bubbling

The event flow proposed by IE is called event bubbling, that is, the event starts with the most specific element Receive, and then propagate up to less specific nodes step by step

p——>body——>html——>document

Second type: event capture

Less specific nodes should receive events earlier, and the most specific nodes should receive events last. The purpose of capture is to capture an event before it reaches its intended destination.

document——>html——>body——>p

DOM event flow

"DOM2 level event" specified The event flow includes three stages: event capture stage, target stage, event bubbling stage

In the DOM event flow, the actual target will not receive events during the capture phase, which means that during the capture phase, The event stops after it reaches the body. The next stage is in the target stage, so the event occurs on p and is considered part of the bubbling stage in event processing. Then, the bubbling phase occurs and the event is propagated back to the document.

Even though the "DOM2-level events" specification clearly requires that the capture phase does not involve the event target, current mainstream browsers will trigger events on the event object during the capture phase. The result is that there are two opportunities to manipulate events on the target object.

DOM level 2 event handler

DOM level 2 events define two methods: operations for processing add events and deletion events:

Add event addEventListener() Delete event removeEventListener()

All DOM nodes contain these two methods, and they both contain 3 parameters: (1) The event method to be processed (for example : click, mouseover, dbclick...) (2) The event processing function can be an anonymous function or a named function (but if you need to delete the event, it must be a named function) (3) A Boolean value, representing Is it in the event bubbling stage processing or event capturing stage (true: means calling the event handler in the capturing stage; false: means calling the event handler in the bubbling stage)

//这是一个DOM 2级事件 添加事件最简单的方式(此时添加的是一个匿名函数)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

    <button>按钮</button>
    <script>
        var btn=document.querySelector(&#39;button&#39;);
        btn.addEventListener(&#39;click&#39;,function(){
            console.log(&#39;我是按钮&#39;)
        },false)   //当第三个参数不写时,也是默认为false(冒泡时添加事件)
    </script>

</body>
</html>
//添加的函数是命名函数,removeEventListener需要用这种方式
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

    <button>按钮</button>
    <script>
        var btn=document.querySelector(&#39;button&#39;);
        btn.addEventListener(&#39;click&#39;,foo,false);
        function foo(){
            console.log(&#39;我是按钮&#39;)
        }
           //其实操作就是把写在里面的函数拿到了外面,而在原来的位置用函数名来代替
    </script>
</body>
</html>

DOM2-level handler also supports adding two If an event handles an event, both events will be executed.

In most cases, we add the event handler to the bubbling phase of the event stream, so as to maximize compatibility. Various browsers.

It is best to only add event handlers to the capture phase when it is necessary to intercept the event before it reaches the target.

If it is not specifically needed, it is not recommended to register an event handler during the event capture phase.

Prevent event bubbling

It is mainly used to prevent event propagation. Prevents it from being dispatched to other DOM nodes and can be used at any stage of event propagation. The usage method is as follows (compatible with IE):

function stopBubble(event){
	if(window.event){
        //兼容IE
		window.event.cancelBubble=true;
	}else{
		event.stopPropagation();
	}
}

Block default events

function stopDefaultEvent(event){
	if(window.event){
        //兼容IE
		window.event.returnValue=false;
	}else{
		event.preventDefault()
	}
	return false;
}

Related articles:

Detailed explanation of JS bubbling events and event capture examples

How to implement JavaSript event bubbling and event capturing

Related videos:

JS Abstract Class and Event Design Pattern Video Tutorial

The above is the detailed content of [JavaScript] Two completely opposite event flows: event bubbling and event capturing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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