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
740

HTML:

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)
曾经蜡笔没有小新
document.getElementById('container').addEventListener('click',function (e) { document.getElementById('inner').style.display = "none"; e.stopPropagation(); }, true);
  1. Pass the third parametertruetoaddEventListener. 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"; });
      仅有的幸福

      Addpointer-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
        About us Disclaimer Sitemap
        php.cn:Public welfare online PHP training,Help PHP learners grow quickly!