[英]Close modal when user clicks outside of it, why is there == and not !=?
我从这里看了一些代码: https : //www.w3schools.com/howto/howto_css_modals.asp ,但是我想理解为什么下面的代码中==而不是!=:
// Get the modal
var modal = document.getElementById('yourModalId');
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
单击模式的外部时,我们想关闭窗口。 为什么那里有一个等号呢?
看起来是因为modal
是背景,而modal-content
是您认为应该是模态的东西。
请参阅此处的工作示例,并注意以下CSS块:
/* The Modal (background) */
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
padding-top: 100px; /* Location of the box */
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 */
.modal-content {
background-color: #fefefe;
margin: auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
}
根据提供的链接, modal
是modal
的灯箱。 (透明背景)因此,当用户单击时,请关闭模式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.