gpt4 book ai didi

javascript - Modal moSTLy 保持立即退出

转载 作者:行者123 更新时间:2023-11-28 03:56:59 26 4
gpt4 key购买 nike

我想在我的代码中包含一个模式。单击按钮/href 时,模式中应显示不同的文件。我只是从 here 中获取了代码.

现在,在加载动画后(或几毫秒后)模态窗口就关闭了。奇怪的是,当你按下按钮几次时,它偶尔会起作用并显示文件。

这是我的代码(相关部分):

// Get the modal
var modal = document.getElementById('myModal');

// Get the button that opens the modal
var btn = document.getElementById("myBtn");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks on the button, open the modal
btn.onclick = function() {
modal.style.display = "flex";
};

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
};

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
};
/* The Modal (background) */
.modal {
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 {
position: relative;
background-color: #fefefe;
margin: auto;
padding: 0;
border: 1px solid #888;
width: 80%;
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
}

/* Modal Header */
.modal-header {
padding: 2px 16px;
background-color: #5cb85c;
color: white;
}

/* Modal Footer */
.modal-footer {
padding: 2px 16px;
background-color: #5cb85c;
color: white;
}

/* The Close Button */
.close {
color: #aaa;
float: right;
font-size: 28px;
font-weight: bold;
}

.close:hover,
.close:focus {
color: black;
text-decoration: none;
cursor: pointer;
}
<div class="nav1">

<a href="index.php?page=users&action=login" id="myBtn">Login</a>

</div>

<div id="myModal" class="modal">
<div class="modal-content">
<div class="modal-header">
<span class="close">&times;</span>
<h2>Modal Header</h2>
</div>
<div class="modal-body">
Modal Body
<?php
if (isset($_GET["page"]) ) {
switch ($_GET["page"]) {
case "post":
include "./system/post/index.php";
break;
case "users":
include "./system/users/index.php";
break;
default:
include "start.php";
break;
}
}
else
{
include "start.php";
}
?>

</div>
<div class="modal-footer">
Modal Footer
</div>
</div>
</div>
<script src="./system/style/modal.js"></script>

最佳答案

在我看来,您的问题是事件的传播。每当您单击 btnspan 时,模态窗口都不会显示,因为最终 window.onclick 发生并且模态窗口关闭。因此,解决方案是在 btn/span 点击事件中添加 event.stopPropagation():

btn.onclick = function(event) {
event.stopPropagation(); // stop the event to bubble up
modal.style.display = "flex";
};
span.onclick = function(event) {
event.stopPropagation(); // stop the event to bubble up
modal.style.display = "none";
};
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
};

关于javascript - Modal moSTLy 保持立即退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43409910/

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