Event piercing is not an event bubbling 1 event bubbling event bubbling exists iflevel nesting
For example: In this case two elements are bound to the Click event or the Touchstart event, and the event will bubble to Dow when clicked on
<div class="dow"> <div id=app> </div> </div>
For example: In this case two elements are bound to the Click event or the Touchstart event, the event does not bubble when clicked on the app
<div id=app> </div> <div class="dow"> </div>
Summary: Because the body is the parent level of all DOM, any event will bubble to the body
2 Event penetration exists premises:
- Upper bound Touchstart Event
- Lower bound Click event
- Hide yourself when you click on the upper layer
- is not related to whether two elements are nested or upper layer
Cause: Touchstart first with click execution
Solution:
app.addEventListener('touchstart', function (e) { app.style.display = 'none' console.log('app click') //重点 e.preventDefault(); //组织事件默认事件,在事件touchstart时候就组织了继续往下产生click\touchend事件 e.stopPropagation();//非必须 此方法主要组织冒泡,由于前面结论可知,仅当dom存在嵌套下才会产生冒泡才需要此方法 })
"E.preventdefault (); Organize event default events, and organize a continuation of the Click\touchend event "at the time of the event Touchstart" explanation
<div class="dow"> <div id=app> </div> </div> app.addEventListener('touchstart', function (e) { app.style.display = 'none' console.log('app touchstart') e.preventDefault(); e.stopPropagation(); }) app.addEventListener('touchend', function (e) { console.log('app end') }) app.addEventListener('click', function (e) { console.log('app click') }) dow.addEventListener('touchstart', function () { console.log('dow touchstart') }) dow.addEventListener('click', function () { console.log('dow click') }) dow.addEventListener('touchend', function (e) { console.log('dow touchend') })
Run Click Results
- App Touchstart
- App End
- Dow Touchend
Translation translation
-clicking once on a page will produce ' touchstart '----' touchend '----' click ' Event
We bind the app ' Touchstart '----' touchend '----' click ' and bind two blocking methods to Touchstart
When we click on the app
Generates the Touchstart event, which organizes the default event for the event, so the click event is no longer generated and does not bubble
Generate Touchend events,
Bubbling Touchend event to Dow
Summary of the mobile "piece piercing" problem