[英]EventListener in JavaScript
I'm a beginner with Javascript and I'm stuck at something.我是 Javascript 的初学者,我被困在某事上。 Here's the code:这是代码:
const button = document.querySelector('button')
const popupWrapper = document.querySelector('.popup-wrapper')
const popBox = document.querySelector('.popup-box')
const close = document.querySelector('.popup-close')
Notice: the whole wrapper is displayed to none, and the .popup-box is inside if the .popup-wrapper div (in case you didn't include that already:D)注意:整个包装器显示为无,如果 .popup-wrapper div 则 .popup-box 在里面(如果你还没有包含它:D)
button.addEventListener('click', () =>{
popupWrapper.style.display = 'block';
});
close.addEventListener('click', () => {
popupWrapper.style.display = 'none';
});
popupWrapper.addEventListener('click', () => {
popupWrapper.style.display = 'none';
});
What I want to do is whenever I click anywhere on the wrapper space around the .popup-box
itself, the whole wrapper (with the box) gets displayed to none, but when I click on .popup-box
itself nothing happens.我想要做的是,每当我点击.popup-box
周围的包装空间上的任何地方时,整个包装(带盒子)都会显示为无,但是当我点击.popup-box
本身时,什么也没有发生。 What do I write?我写什么?
addEventListener callback takes an event as it's argument, so if you want to prevent event's propagation ( prevents further propagation of the current event in the capturing and bubbling phases
), you can use (event) => event.stopPropagation();
addEventListener 回调将事件作为参数,所以如果你想阻止事件的传播( prevents further propagation of the current event in the capturing and bubbling phases
),你可以使用(event) => event.stopPropagation();
like here:像这儿:
popupBox.addEventListener('click', (event) => {
event.stopPropagation();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.