国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

javascript - If the parent element is bound to click, how can the child element avoid triggering the click of the parent element without binding click?
扔個三星炸死你
扔個三星炸死你 2017-06-12 09:22:56
0
3
1048

HTML:

<p id="container">
  <p id="inner">

  </p>
</p>

JS:

document.getElementById('container').addEventListener('click',function () {
  document.getElementById('inner').style.display = "none";
});

When I click on the child element, it will disappear. How to avoid this situation? I don't want to bind click events to child elements as well.

扔個三星炸死你
扔個三星炸死你

reply all(3)
曾經(jīng)蠟筆沒有小新
document.getElementById('container').addEventListener('click',function (e) {
  document.getElementById('inner').style.display = "none";
  e.stopPropagation();
}, true);
  1. Pass the third parameter true to addEventListener. Use event capture.

    • https://developer.mozilla.org...

  2. e.stopPropagation() Stop event propagation.

    • https://developer.mozilla.org...

僅有的幸福

https://jsfiddle.net/g5u7qrrd/6/

document.getElementById('container')
    .addEventListener('click',function (e) {
        if (e.target.id !== 'inner')
            document.getElementById('inner').style.display = "none";
    });
僅有的幸福

Add pointer-events: none;to the sub-element style and ignore mouse events directly. IE may need to be compatible.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template