简体   繁体   English

处理内部 div 和外部 div 上的点击事件处理程序

[英]Dealing with click event handler on inner div and outer div

I've got modal window: parent div (wrapper with dark transparent background) and child div (with buttons, inputs and text).我有模态 window:父div (具有深色透明背景的包装器)和子div (带有按钮、输入和文本)。 Both of them are fixed positioning.两者都是固定定位。
In JavaScript I do simple code: If I click on parent fixed div (.modal-wrapper) - it must close (remove) whole modal window.在 JavaScript 我做简单的代码:如果我点击父固定 div(.modal-wrapper) - 它必须关闭(删除)整个模态 window。 And same result if I click on close button ('X' button in the corner of modal window).如果我单击关闭按钮(模式窗口角落的“X”按钮),结果相同。

Here is my simple JS code:这是我的简单 JS 代码:

function doModalClose() {
   modalWrapper.remove();
}

closeBtn.addEventListener('click', doModalClose);
modalWrapper.addEventListener('click', doModalClose);

And html markup:和 html 标记:

<div class="modal-wrapper">
    <div class="modal-window">
        <div class="btn-wrapper"><span class="close-btn"></span></div>
        other modal elements...
    </div>
</div>

The main question is that when I click on parent div it works correctly, and as when I click on .close-btn .主要问题是,当我点击父div时,它可以正常工作,就像我点击.close-btn时一样。 But if I clicked on child div (.modal-window) it closes whole modal too, which incorrect!但是如果我点击子div (.modal-window) 它也会关闭整个模式,这是不正确的!

I make a simple alert();我做了一个简单的alert(); check: when I click on parent one it should say Modal Wrapper and when I click on child one it should say Modal Window .检查:当我单击父级时,它应该说Modal Wrapper ,当我单击子级时,它应该说Modal Window
And when I click on child window it calls two alert();当我点击子 window 时,它会调用两个alert(); functions: first is child's Modal Window and after that parent's Modal Wrapper .功能:首先是孩子的Modal Window ,然后是父母的Modal Wrapper
So that's the reason of my issue, but I don't understand the reason of that kind of behavior.所以这就是我的问题的原因,但我不明白这种行为的原因。 I use on child div z-index: 2;我在子 div z-index: 2;上使用and z-index: 1;z-index: 1; on parent div, but it still doesn't help.在父 div 上,但它仍然没有帮助。

Thank you for your attention!感谢您的关注!

This happens because one is inside another one, if you click the inner div you also click the outer one because the inner is inside the outer one.发生这种情况是因为一个在另一个内部,如果单击内部 div,您也会单击外部 div,因为内部在外部内部。 You need an extra handler just for .modal-window and to use event.stopPropagation() .您需要一个额外的处理程序,仅用于.modal-window并使用event.stopPropagation() Run this code snippet and see by yourself.运行此代码段并自行查看。

 function doModalClose() { modalWrapper.remove(); } var closeBtn = document.getElementsByClassName('close-btn')[0] var modalWrapper = document.getElementsByClassName('modal-wrapper')[0] var modalWindow = document.getElementsByClassName('modal-window')[0] var button1 = document.getElementsByClassName('button1')[0] var button2 = document.getElementsByClassName('button2')[0] closeBtn.addEventListener('click', doModalClose); modalWrapper.addEventListener('click', doModalClose); modalWindow.addEventListener('click', function (event) { event.stopPropagation() }) button1.addEventListener('click', function (event) { alert('button1') }) button2.addEventListener('click', function (event) { alert('button2') })
 div {margin: 5px; padding: 5px}.modal-wrapper {border: 2px solid red}.modal-window {border: 2px solid blue}.btn-wrapper, .btn {border: 2px solid green}
 <div class="modal-wrapper">modal-wrapper <div class="modal-window">modal-window <div class="btn-wrapper"><span class="close-btn">button</span></div> <a class="btn button1">Button1</a> <a class="btn button2">Button2</a> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM