【发布时间】:2021-01-25 20:12:23
【问题描述】:
我为我的网站设置的模式打开,但当我点击 X 按钮或外部时不关闭。
当我在浏览器中运行我的代码时,我可以单击该按钮并且它可以工作。但随后它不会关闭。我不确定错误是什么或为什么这不起作用。
var modal = document.getElementById("WhiteSedan1")
var btn1 = document.getElementById("BtnWhiteSedan")
var span = document.getElementsByClassName("close")[0];
btn1.onclick = function() {
modal.style.display = "block";
}
span.onclick = function() {
modal.style.display = "none";
}
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
.modal {
display: none;
/* Hidden by default */
position: fixed;
/* Stay in place */
z-index: 1;
/* Sit on top */
padding-top: 50x;
/* Location of the box */
left: 0;
top: 0;
width: 50%;
/* 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);
/* Black w/ opacity */
}
/* Modal Content */
.modal-content {
background-color: #fefefe;
margin: auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
}
/* The Close Button */
.close {
color: #aaaaaa;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
<div class="desc">
<a href="#" class="btn btn-outline-primary" id="BtnWhiteSedan">White Sedan</a>
</div>
<div id="WhiteSedan1" class="modal">
<div class="modal-content">
<span class="close">×</span>
<p class="text-center">
Model: Toyota<br> Mileage: 28,000 km <br> Transmission: Auto<br> Cost: $10,000
</p>
</div>
</div>
我的目标是当我点击 X 或模态框外时,模态框关闭。
【问题讨论】:
-
×按钮工作正常。padding-top: 50x;是一个错字。使用addEventListener而不是onclick。 -
“x”按钮是功能性的,而且由于您的 CSS,您只能单击模式的背景。检查此回复以进行澄清repl.it/join/rtrznwll-programmemitch
标签: javascript html css