我想创建一个模态框。当用户单击按钮时,它应该出现,并在用户单击模式框中的关闭按钮时关闭。 我做了两个功能:
为什么关闭功能不起作用以及我应该现代化以获得结果?
提前致谢)
function check() {
var modal = document.getElementById("myModal");
modal.style.display = "block";
}
//this function dont't work
function close(){
var modal = document.getElementById("myModal");
modal.style.display = "none";
}
.modal {
display: none;
position: fixed;
z-index: 1;
padding-top: 100px;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgb(0,0,0);
background-color: rgba(0,0,0,0.4);
}
.modal-content {
background-color: #fefefe;
margin: auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
}
<html>
<body>
<button id="button" type="submit" onclick="check()">Login</button>
<div id="myModal" class="modal">
<div class="modal-content" >
<span class="close" onclick="close()">×</span>
<p >Some text in the Modal..</p>
</div>
</div>
</body>
</html>
答案 0 :(得分:3)
window.close()
已经是本机浏览器功能function check() {
var modal = document.getElementById("myModal");
modal.style.display = "block";
}
function closeModal(){
var modal = document.getElementById("myModal");
modal.style.display = "none";
}
的名称。重命名你的功能,它工作正常:
.modal {
display: none;
position: fixed;
z-index: 1;
padding-top: 100px;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgb(0,0,0);
background-color: rgba(0,0,0,0.4);
}
.modal-content {
background-color: #fefefe;
margin: auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
}
&#13;
<html>
<body>
<button id="button" type="submit" onclick="check()">Login</button>
<div id="myModal" class="modal">
<div class="modal-content" >
<span class="close" onclick="closeModal()">×</span>
<p >Some text in the Modal..</p>
</div>
</div>
</body>
</html>
&#13;
of()
&#13;