gpt4 book ai didi

javascript - 模态关闭按钮不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 08:33:34 25 4
gpt4 key购买 nike

这是代码。如果我打开模式,关闭按钮将不起作用,并且在刷新页面之前不会关闭。

// 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 the button, open the modal
btn.onclick = function() {
modal.style.display = "block";
}

// 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";
}
}
/* Create three columns of equal width */

.columns {
float: left;
width: 33.3%;
padding: 8px;
}


/* Style the list */

.price {
list-style-type: none;
border: 1px solid #eee;
margin: 0;
padding: 0;
-webkit-transition: 0.3s;
transition: 0.3s;
}


/* Add shadows on hover */

.price:hover {
box-shadow: 0 8px 12px 0 rgba(0, 0, 0, 0.2)
}


/* Pricing header */

.price .header {
background-color: #111;
color: white;
font-size: 25px;
}


/* List items */

.price li {
border-bottom: 1px solid #eee;
padding: 20px;
text-align: center;
}


/* Grey list item */

.price .grey {
background-color: #eee;
font-size: 20px;
}


/* The "Sign Up" button */

.button {
background-color: #4CAF50;
border: none;
color: white;
padding: 10px 25px;
text-align: center;
text-decoration: none;
font-size: 18px;
}


/* Change the width of the three columns to 100%
(to stack horizontally on small screens) */

@media only screen and (max-width: 600px) {
.columns {
width: 100%;
}
}


/* 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 {
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 */
}


/* 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="columns">
<ul class="price">
<li class="header">Hard Drive Format</li>
<li class="grey">£10</li>
<li>Format Hard Drive</li>
<li>Removes ALL Files From Drive</li>
<li>Fresh Install Windows</li>
<li class="grey"><button onclick="document.getElementById('myModal').style.display='block'">Sign
Up</button></li>
</ul>
</div>

<!-- The Modal -->
<div id="myModal" class="modal">

<!-- Modal content -->
<div class="modal-content">
<span class="close">&times;</span>
<p>Some text in the Modal..</p>
</div>

</div>

我只想在单击按钮时关闭模式。

最佳答案

在你的代码中没有id为myBtn的元素

所以去掉下面的代码

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

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

它会很好地工作

关于javascript - 模态关闭按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44630666/

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