gpt4 book ai didi

javascript - 多个模态只能关闭1个

转载 作者:行者123 更新时间:2023-11-28 14:07:21 26 4
gpt4 key购买 nike

我已经成功在我的网站上创建了多个模态,但是我只能关闭三个模态中的最后一个,即使它们具有 3 个独立的函数和 3 个不同的类。

如何才能使每个模式都通过相应的按钮打开,并在您单击模式窗口外时关闭。

HTML

<div class="container flex bg-black-opacity mt-40 rounded pt-10 pb-12 mb-10 mx-auto items-center">
<div class="container ml-5 cursor-pointer" id='1btn'>
<img src="assets/img/merch-1.png" alt="" style='height: 400px;' class='shadow-pink'>
<p class='uppercase mt-4 text-white'>Vice Versa 2020 Heather Grey Sweatshirt</p>
<p class="text-white mt-4 ml-1 text-2xl">£15.00</p>

</div>
<div id="1mdl" class="modal1">

<!-- Modal content -->
<div class="modal-content">
<p>Some text in the Modal..</p>
</div>
</div>
<div class="container ml-5 cursor-pointer" id='2btn'>
<img src="assets/img/merch-2.png" alt="" style='height: 400px;' class='shadow-pink'>
<p class='uppercase mt-4 text-white'>Vice Versa 2020 Heather Grey Sweatshirt</p>
<p class="text-white mt-4 ml-1 text-2xl">£15.00</p>

</div>
<div id="2mdl" class="modal2">

<!-- Modal content -->
<div class="modal-content">
<p>Some text in the Modal..</p>
</div>
</div>
<div class="container ml-5 cursor-pointer" id='3btn'>
<img src="assets/img/merch-3.png" alt="" style='height: 400px;' class='shadow-pink'>
<p class='uppercase mt-4 text-white'>Vice Versa 2020 Heather Grey Sweatshirt</p>
<p class="text-white mt-4 ml-1 text-2xl">£15.00</p>

</div>
<div id="3mdl" class="modal3">

<!-- Modal content -->
<div class="modal-content">
<p>Some text in the Modal..</p>
</div>
</div>

</div>

CSS

 .modal1 {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}
.modal2 {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}
.modal3 {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}

/* Modal Content/Box */
.modal-content {
background-color: #fefefe;
margin: 15% auto; /* 15% from the top and centered */
padding: 20px;
border: 1px solid #888;
width: 80%; /* Could be more or less, depending on screen size */
}

JavaScript

var modal1 = document.getElementById("1mdl");
var modal2 = document.getElementById("2mdl");
var modal3 = document.getElementById("3mdl");

var btn1 = document.getElementById("1btn");
var btn2 = document.getElementById("2btn");
var btn3 = document.getElementById("3btn");

btn1.onclick = function() {
modal1.style.display = "block";
}
btn2.onclick = function() {
modal2.style.display = "block";
}
btn3.onclick = function() {
modal3.style.display = "block";
}
window.onclick = function(event) {
if (event.target == modal1) {
modal1.style.display = "none";
}
}
window.onclick = function(event) {
if (event.target == modal2) {
modal2.style.display = "none";
}
}
window.onclick = function(event) {
if (event.target == modal3) {
modal3.style.display = "none";
}
}

最佳答案

重新分配点击事件会覆盖之前绑定(bind)的回调函数

只需使用它(保持原始问题中的简化)

 window.onclick = function(event) {
if (event.target == modal1) {
modal1.style.display = "none";
} else if (event.target == modal2) {
modal2.style.display = "none";
} else
if (event.target == modal3) {
modal3.style.display = "none";
}
}

关于javascript - 多个模态只能关闭1个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60789961/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com