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.
Pass the third parameter
true
toaddEventListener
. Use event capture.https://developer.mozilla.org...
e.stopPropagation()
Stop event propagation.https://developer.mozilla.org...
https://jsfiddle.net/g5u7qrrd/6/
Add
pointer-events: none;
to the sub-element style and ignore mouse events directly. IE may need to be compatible.