您可以通过以下步骤使用JavaScript创建简单的模态确认框:
<div id="myModal" class="modal">
<div class="modal-content">
<h3>确认要执行此操作吗?</h3>
<div>
<button id="confirmBtn">确认</button>
<button id="cancelBtn">取消</button>
</div>
</div>
</div>
.modal {
display: none;
position: fixed;
z-index: 1;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgba(0,0,0,0.4);
}
.modal-content {
background-color: #fefefe;
margin: 15% auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
text-align: center;
}
button {
background-color: #4CAF50;
color: white;
padding: 10px 20px;
border: none;
cursor: pointer;
margin-right: 10px;
}
button:hover {
background-color: #3e8e41;
}
var modal = document.getElementById("myModal");
var confirmBtn = document.getElementById("confirmBtn");
var cancelBtn = document.getElementById("cancelBtn");
function showModal() {
modal.style.display = "block";
}
function hideModal() {
modal.style.display = "none";
}
confirmBtn.onclick = function() {
// 执行确认操作
hideModal();
}
cancelBtn.onclick = function() {
// 取消操作
hideModal();
}
<button onclick="showModal()">执行操作</button>
这就是使用JavaScript创建简单的模态确认框的步骤。您可以根据需要对HTML、CSS和JavaScript进行自定义,以满足您的特定需求。