简体   繁体   English

添加点击事件以隐藏弹出窗口

[英]Adding a click event to hide a popup

I have a popup with an id of myPopup .我有一个 id 为myPopup的弹出窗口。
show is a class that changes the display from none to block. show是一个 class 将显示从无更改为阻塞。
This is my first time using event handlers and I was looking to get some information on the different types and how to use them.这是我第一次使用事件处理程序,我希望获得一些关于不同类型以及如何使用它们的信息。

var popup_v = document.getElementById("myPopup");

function popup() {
    popup_v.classList.toggle("show");
}
if(document.getElementById('myPopup').classList.contains("show")) {
    document.addEventListener('click', function(event) {
        popup_v.classList.remove("show");
    });
}
var popup_v = document.getElementById("myPopup");

function popup() {
    popup_v.classList.toggle("show");
}
if(document.getElementById('myPopup').classList.contains("show")) {
    document.addEventListener('click', function(event) {
        popup_v.classList.remove("show");
    });

So first of all your if statement will only run once and since the class is only toggled during the function, then the if statement will never be run and so the event won't actually happen.因此,首先您的 if 语句只会运行一次,并且由于 class 仅在 function 期间切换,因此 if 语句将永远不会运行,因此该事件实际上不会发生。 To fix this maybe try this approach:要解决此问题,请尝试以下方法:

var popup_v = document.getElementById("myPopup");

var trueorfalse = false;

function popup() {
    if (trueorfalse === false) }
     trueorfalse = true; // popup is there
     popup_v.classList.toggle("show");
    } else {
     popup_v.classList.remove("show");
    }
}
if(document.getElementById('myPopup').classList.contains("show")) {
    document.addEventListener('click', function(event) {
        popup_v.classList.remove("show");
    });

OR或者

var popup_v = document.getElementById("myPopup");

function popup() {
    popup_v.classList.toggle("show");
}
var trueorfalse = false;
document.addEventListener('click', function(event) {
 if (trueorfalse === false) }
  trueorfalse = true; // popup is there
  popup_v.classList.toggle("show");
 } else {
  popup_v.classList.remove("show");
 }
}
};

I did it these ways because the event listener just checks if the event is happening for how long the application (site) is run.我这样做是因为事件侦听器只是检查事件是否在应用程序(站点)运行多长时间内发生。

So you can just check if the class is there or not and then decide depending on that.所以你可以检查 class 是否存在,然后根据它来决定。

Tell me if anything is wrong.告诉我有什么问题。

I don't see a need to add the if condition specifically to check if it contains class and it it has remove it.我认为不需要专门添加 if 条件来检查它是否包含 class 并且它已将其删除。

the toggle method should do both for you, ie on toggle add show class, on toggle again if show class is present remove it切换方法应该为您做这两件事,即在切换时添加显示 class,如果显示 class 存在则再次切换

check this example for reference - https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_toggle_class检查此示例以供参考 - https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_toggle_class

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

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