Summary of the mobile "piece piercing" problem

Source: Internet
Author: User

Overall code
<! DOCTYPE html>
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.