标签:ons get 说明 script hand capture handler nbsp listen
事件执行分捕获阶段和冒泡阶段,都可以使用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>event</title> <style> #outer{ text-align: center; width: 400px; height: 400px; background-color: #ccc; margin: 0 auto; } #middle{ width: 250px; height: 250px; background-color: #f00; margin: 0 auto; } #inner{ width: 100px; height: 100px; background-color: #0f0; margin: 0 auto; } </style> </head> <body> <div id=‘outer‘> <span>outer</span> <div id=‘middle‘> <span>middle</span> <div id=‘inner‘> <span>inner</span> </div> </div> </div> <script> function $(element){ return document.getElementById(element); } function on(element,event_name,handler,use_capture){ if(addEventListener){ $(element).addEventListener(event_name,handler,use_capture); } else{ $(element).attachEvent(‘on‘+event_name,handler); } } on("outer","click",o_click_c,true); on("middle","click",m_click_c,true); on("inner","click",i_click_c,true); on("outer","click",o_click_b,false); on("middle","click",m_click_b,false); on("inner","click",i_click_b,false); function o_click_c(e){ e.stopPropagation() console.log("outer_捕获"); alert("outer_捕获"); } function m_click_c(){ console.log("middle_捕获"); alert("middle_捕获"); } function i_click_c(){ console.log("inner_捕获"); alert("inner_捕获"); } function o_click_b(){ console.log("outer_冒泡"); alert("outer_冒泡"); } function m_click_b(){ console.log("middle_冒泡"); alert("middle_冒泡"); } function i_click_b(){ console.log("inner_冒泡"); alert("inner_冒泡"); } </script> </body> </html>
这个例子说明,事件在捕获阶段被阻止了 ,后续的捕获和冒泡都不会被执行
标签:ons get 说明 script hand capture handler nbsp listen
原文地址:https://www.cnblogs.com/pangmin/p/9780289.html