简体   繁体   English

在香草 javascript 中单击叠加层时如何隐藏模式

[英]How do I hide a modal when the overlay is clicked in vanilla javascript

I'm trying to figure out how to hide the div which is the overlay only when the overlay is clicked and not any of its children (ie: the content):我试图弄清楚如何仅在单击覆盖而不是其任何子项(即:内容)时隐藏作为覆盖的 div:

modal.addEventListener('click', destroy, false);


function destroy(e) {
    if (e.target.id === overlay.id) {
        window.location.reload(false);
    }
}

Your if condition is working but you don't hide something.您的if条件有效,但您没有隐藏任何东西。 For that you could add a class (for example .hidden ) and define for that class display: none .为此,您可以添加一个 class (例如.hidden )并为该 class display: none

Working example:工作示例:

 const modal = document.querySelector('#modal'); const overlay = document.querySelector('#overlay'); modal.addEventListener('click', destroy, false); function destroy(e) { if (e.target.id === overlay.id) { e.target.classList.add('hidden'); } }
 #overlay { height: 50px; background-color: yellow; } p { background-color: red; }.hidden { display: none; }
 <div id="modal"> <div id="overlay"> <p>test</p> </div> </div>

暂无
暂无

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

相关问题 如何使用 vanilla javascript 将 url 打开到覆盖层中? - How do I open a url into an overlay using vanilla javascript? 在javascript中单击按钮时如何显示模态? 我需要一个例子,谢谢 - how do you Display a modal when a button is clicked in javascript? i need a example, thanks 当点击它之外的任何区域时,如何使用javascript / jquery隐藏此下拉菜单? - How do I hide this drop down menu using javascript/jquery when any area outside of it is clicked? 请单击浏览器后退按钮时如何使用 Vanilla Javascript 重定向页面? - Pls how can i redirect a page using Vanilla Javascript when the browser back button is clicked? 单击禁用按钮时如何显示模式? - How do I make a modal appear when the disabled button is clicked? 单击按钮时如何将此 animation class 添加到模态? - How do I add this animation class to a modal when a button is clicked? 关闭模式叠加层时无法隐藏消息框 - Cannot hide message box when I close modal overlay 如何使 function 仅在原版 Javascript 中单击 3 个按钮时触发? - How to make a function only fire when 3 buttons are clicked in Vanilla Javascript? 单击父级时如何不触发其子级? (香草JavaScript) - how to not fire its children when parent is clicked? (vanilla javascript) 在Vanilla JavaScript中,如何显示/隐藏<span>多个类名?</span> - In Vanilla JavaScript, how do I show/hide <span>s with multiple class names?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM