Home  >  Article  >  Web Front-end  >  JavaScript event model (graphic tutorial)

JavaScript event model (graphic tutorial)

亚连
亚连Original
2018-05-19 10:09:111541browse

This article mainly introduces the event model in JavaScript, which includes the DOM0-level event model and the DOM2-level event model (event capture and event bubbling and DOM2-level registration events and deactivation events). Friends who need it can refer to it. There are two event models in

javascript: DOM0 and DOM2. As for these two time models, I have never been very clear about them. Now I finally understand a little more by looking up information online.

1. DOM0-level event model

The DOM0-level event model is an early event model and is supported by all browsers. Its implementation is also relatively simple. The code is as follows:


<p id = &#39;click&#39;>click me</p>
<script>
 document.getElementById(&#39;click&#39;).onclick = function(event){
  alert(event.target);
 }
</script>


This event model is to register the event name directly on the dom object. This code is on the p tag. An onclick event is registered, and the clicked target is output inside this event function. Dismissing the event is even simpler, just copy null to the event function, as follows:

document.getElementById('click'_).onclick = null;
From this we can We know that in dom0, a dom object can only register one function of the same type, because if multiple functions of the same type are registered, overwriting will occur and the previously registered functions will be invalid.


var click = document.getElementById(&#39;click&#39;);
click.onclick = function(){
 alert(&#39;you click the first function&#39;);
};
click.onclick = function(){
 alert(&#39;you click the second function&#39;)
}


In this code, we registered two onclick functions for the dom object, but the result was that only the second one was executed. A registered function, the previously registered function is overwritten.

2. DOM2-level event model

 1. Event capture and event bubbling (capture, bubble)

First of all, IE8 and below do not support this event model. The mechanism of event capture and event bubbling is as follows:

As shown in the figure above, 123 represents event capture and 4567 represents event bubbling. First we use the following code:


<p id = &#39;outer&#39; style = &#39;margin: 100px 0 0 100px; width: 200px;height: 200px; background: red;&#39;>
 <p id="inner" style = &#39;margin-left:20px; width: 50px;height:50px; background: green;&#39;></p>
</p>


## Suppose we click on p with the ID of inner, then the event at this time The process is to first execute the capture phase: document-html-body-p(outer). Then execute the bubbling phase: p(inner)-p(outer)-body-html-document.

 

2. DOM2-level registration events and deactivation events

Use addEventListener and removeEventListener in DOM2 level to register and deactivate events (IE8 and Not supported in previous versions). The advantage of this function compared to the previous method is that a DOM object can register multiple events of the same type, and event overwriting will not occur, and each event function will be executed in sequence.

 addEventListener('event name','event callback','capture/bubble'). The example is as follows:


<p id = &#39;outer&#39; style = &#39;margin: 100px 0 0 100px; width: 200px;height: 200px; background: red;&#39;>
 <p id="inner" style = &#39;margin-left:20px; width: 50px;height:50px; background: green;&#39;></p>
</p>
<script>
 var click = document.getElementById(&#39;inner&#39;);
 click.addEventListener(&#39;click&#39;,function(){
  alert(&#39;click one&#39;);
 },false);
 click.addEventListener(&#39;click&#39;,function(){
  alert(&#39;click two&#39;);
 },false);
</script>


First we need to know that the first parameter of addEventListenr is the event name, which is different from DOM level 0." on", and the third parameter represents capture or bubbling, true represents the capture event, and false represents the bubbling event.

In this code, we registered two click event functions for inner p. The result is that the browser will execute these two functions in sequence.

Next we demonstrate how to use the event stream generation mechanism.


<p id = &#39;outer&#39; style = &#39;margin: 100px 0 0 100px; width: 200px;height: 200px; background: red;&#39;>
 <p id="inner" style = &#39;margin-left:20px; width: 50px;height:50px; background: green;&#39;></p>
</p>
<script>
 var click = document.getElementById(&#39;inner&#39;);
 var clickouter = document.getElementById(&#39;outer&#39;);
 click.addEventListener(&#39;click&#39;,function(){
  alert(&#39;inner show&#39;);
 },true);
 clickouter.addEventListener(&#39;click&#39;,function(){
  alert(&#39;outer show&#39;);
 },true);
</script>


In this code, we use capture events. Since inner is nested in outer, we know when to use When capturing, the outer should capture the event first, and then the inner can capture the event. Then the result is that outer is executed first, followed by inner.


So what if I change the execution timing of outer to the bubbling stage?


alickouter.addEventListener(&#39;click&#39;,function(){
 alert(&#39;outer show&#39;); 
},false);


In this case, the inner is executed first and then the outer. In the same way, if we change the execution timing of both events to the bubbling stage, the inner will still be executed first and then the outer. Then there is another problem, that is, if we register two click events in inner, one is in the capturing phase and the other is in the bubbling phase, that is to say, the third parameter of addEventListenter is set to false and true respectively, then execute What is the order of .


<script>
 var click = document.getElementById(&#39;inner&#39;);
 var clickouter = document.getElementById(&#39;outer&#39;);
 click.addEventListener(&#39;click&#39;,function(){
  alert(&#39;capture show&#39;);
 },true);
 click.addEventListener(&#39;click&#39;,function(){
  alert(&#39;bubble show&#39;);
 },false);
</script>


In this case, the first is the capture show, followed by the bubble show. However, this result is related to the order of registration. The first to register will be executed first. Because we are looking at the diagram of event capture and event bubbling, we find that there is only one specific DOM object in the end.


So what if we register click events for both outer and inner but I don’t want outer to execute? At this time we need to use the stopPropagation function. This function is used to prevent bubbling. The implication is that the event will no longer continue to bubble, so that the DOM object that registers the same type of event will not be executed.

比如在自制下拉框的时候,我们点击浏览器的其他位置,我们需要下拉框的options隐藏,这时我们就要用到stopPropagation了。如下:


<script>
 var click = document.getElementById(&#39;inner&#39;);
 var clickouter = document.getElementById(&#39;outer&#39;);
 click.addEventListener(&#39;click&#39;,function(event){
  alert(&#39;inner show&#39;);
  event.stopPropagation();
 },false);
 clickouter.addEventListener(&#39;click&#39;,function(){
  alert(&#39;outer show&#39;);
 },false);
</script>


  正常的情况下,我们在不添加stopPropagation函数时,首先应该执行inner,然后执行outer,但是当我们在inner的事件函数中添加了stopPropagation函数之后,执行完inner的事件函数之后,就不会在执行outer的事件函数了,也可以理解为事件冒泡到inner之后就消失了,因此也就不会在执行接下来的事件函数了。

  由于事件捕获阶段没有可以阻止事件的函数,所以一般都是设置为事件冒泡。

好了以上就是全部内容啦 ,希望对大家的学习有所帮助~~


上面是我整理给大家的,希望今后会对大家有帮助。

相关文章:

Vue.js使用开源框架mpvue

p5.js如何进行图片加载

js如何下载图片到本地


The above is the detailed content of JavaScript event model (graphic tutorial). 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