在JavaScript中,弹出框(也称为对话框或模态框)是一种常见的用户界面元素,用于显示额外的信息,或要求用户输入数据。弹出框可以是简单的警告框、确认框,或者是更复杂的自定义模态窗口。
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;
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%;
}
</style>
</head>
<body>
<h2>Custom Modal Example</h2>
<button id="myBtn">Open Modal</button>
<div id="myModal" class="modal">
<div class="modal-content">
<p>Some text in the Modal..</p>
<button id="closeBtn">Close</button>
</div>
</div>
<script>
var modal = document.getElementById("myModal");
var btn = document.getElementById("myBtn");
var span = document.getElementById("closeBtn");
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>
如果你遇到了具体的问题,可以提供更详细的信息,以便给出更精确的解决方案。
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云