在JavaScript中,弹出选择窗口通常是通过window.prompt()
、window.confirm()
和window.alert()
这三个内置函数来实现的,但如果你指的是一个自定义的选择窗口(比如一个模态框),那么可以通过HTML、CSS和JavaScript结合来创建。
模态框(Modal):是一种覆盖在父窗体上的子窗体,通常用于显示额外的信息或者获取用户的输入。模态框会阻止用户与其他界面元素交互,直到模态框被关闭。
alert()
函数,用于显示警告信息。confirm()
函数,用于获取用户的确认或取消操作。prompt()
函数,用于获取用户的输入。以下是一个简单的自定义模态框的示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Custom Modal Example</title>
<style>
.modal {
display: none;
position: fixed;
z-index: 1;
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: 15% auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
}
.close {
color: #aaa;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: black;
text-decoration: none;
cursor: pointer;
}
</style>
</head>
<body>
<h2>Custom Modal Example</h2>
<button id="myBtn">Open Modal</button>
<div id="myModal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<p>Some text in the Modal..</p>
</div>
</div>
<script>
var modal = document.getElementById("myModal");
var btn = document.getElementById("myBtn");
var span = document.getElementsByClassName("close")[0];
btn.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";
}
}
</script>
</body>
</html>
如果你遇到了模态框不显示或者显示异常的问题,可以检查以下几点:
display
属性。display
属性。通过上述方法,你可以创建一个基本的自定义模态框,并根据需要进行样式和功能的调整。
领取专属 10元无门槛券
手把手带您无忧上云