This article analyzes JS bubbling events and event capture with examples. Share it with everyone for your reference, the details are as follows:
Case
<!DOCTYPE html> <html> <head> <title>冒泡事件</title> <script type="text/javascript"> window.onload = function(){ window.onclick = function(){ alert("Window"); // 顶级 }; document.onclick = function(){ alert("Document"); // 次顶级 }; document.documentElement.onclick = function(){ alert("Html"); // 次次顶级 }; document.body.onclick = function(){ alert("Body"); // 次次次顶级 }; document.getElementById("myDiv").onclick = function(){ alert("Div"); // 最先执行,并且会传递到上一层。点击两次,则按顺序执行两次。如果上级也有点击事件的话 }; } </script> </head> <body> <div id="myDiv">点我</div> </body> </html>
Summary
1. Click myDiv. The order is Div-Body-Html-Document-Window
2. Click on other blank places. The order is Html-Document-Window
3. Click twice in succession and it will be executed twice in sequence
4. The premise of bubbling is that the parent also defines the corresponding event
Let’s look at a more complex example
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>事件捕捉</title> <script type="text/javascript"> window.onload = function(){ window.addEventListener("click", function(){ alert("Window - true"); }, true); // true - 事件句柄在捕获阶段执行 ,false- false- 默认。事件句柄在冒泡阶段执行 document.addEventListener("click", function(){ alert("Document - true"); }, true); document.documentElement.addEventListener("click", function(){ alert("Html - true"); }, true); document.body.addEventListener("click", function(){ alert("Body - true"); }, true); document.getElementById("myDiv").addEventListener("click", function(){ alert("Div - true"); }, true); window.addEventListener("click", function(){ alert("Window - false"); }, false); // true - 事件句柄在捕获阶段执行 ,false- false- 默认。事件句柄在冒泡阶段执行 document.addEventListener("click", function(){ alert("Document - false"); }, false); document.documentElement.addEventListener("click", function(){ alert("Html - false"); }, false); document.body.addEventListener("click", function(){ alert("Body - false"); }, false); document.getElementById("myDiv").addEventListener("click", function(){ alert("Div - false"); }, false); window.onclick = function(){ alert("Window - click"); // 顶级 }; document.onclick = function(){ alert("Document - click"); // 次顶级 }; document.documentElement.onclick = function(){ alert("Html - click"); // 次次顶级 }; document.body.onclick = function(){ alert("Body - click"); // 次次次顶级 }; document.getElementById("myDiv").onclick = function(){ alert("Div - click"); // 最先执行,并且会传递到上一层。点击两次,则按顺序执行两次。如果上级也有点击事件的话 }; } </script> </head> <body> <div id="myDiv">点我</div> </body> </html>
Summary
1. The execution result of the click is
Window - true
Document - true
Html - true
Body - true
Div - true
Div - false
Div- click
Body - false
Body - click
Html - false
Html - click
Document - false
Document - click
Window - false
Window - click
2. Sequence and js code sequence It's irrelevant
3. Even if the click event is not defined, the click event can still be captured. As long as it is clicked, it can be captured
Reform again to prevent bubbling
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>事件捕捉</title> <script type="text/javascript"> window.onload = function(){ document.addEventListener("click", function(){ alert("Document - true"); }, true); window.addEventListener("click", function(){ alert("Window - true"); }, true); // true - 事件句柄在捕获阶段执行 ,false- false- 默认。事件句柄在冒泡阶段执行 document.documentElement.addEventListener("click", function(){ alert("Html - true"); }, true); document.body.addEventListener("click", function(){ alert("Body - true"); }, true); document.getElementById("myDiv").addEventListener("click", function(){ alert("Div - true"); }, true); window.addEventListener("click", function(){ alert("Window - false"); }, false); // true - 事件句柄在捕获阶段执行 ,false- false- 默认。事件句柄在冒泡阶段执行 document.addEventListener("click", function(){ alert("Document - false"); }, false); document.documentElement.addEventListener("click", function(){ alert("Html - false"); }, false); document.body.addEventListener("click", function(){ alert("Body - false"); }, false); document.getElementById("myDiv").addEventListener("click", function(){ alert("Div - false"); }, false); window.onclick = function(){ alert("Window - click"); // 顶级 }; document.onclick = function(){ alert("Document - click"); // 次顶级 }; document.documentElement.onclick = function(){ alert("Html - click"); // 次次顶级 }; document.body.onclick = function(){ alert("Body - click"); // 次次次顶级 }; document.getElementById("myDiv").onclick = function(){ alert("Div - click"); // 最先执行,并且会传递到上一层。点击两次,则按顺序执行两次。如果上级也有点击事件的话 event.stopPropagation(); // 阻止冒泡 }; } </script> </head> <body> <div id="myDiv">点我</div> </body> </html>
Summary
Window - true
Document - true
Html - true
Body - true
Div - true
Div - false
Div - click
has been terminated, there is no follow-up content