jQuery 弹出确认对话框是一种使用 jQuery 库来实现的用户界面交互功能。它允许开发者在网页上显示一个模态对话框,用于向用户确认某个操作是否继续执行。这种对话框通常包含“确定”和“取消”两个按钮,用户可以选择其中一个选项。
以下是一个使用 jQuery 实现自定义确认对话框的示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery Confirm Dialog</title>
<style>
.confirm-dialog {
display: none;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: white;
border: 1px solid #ccc;
padding: 20px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.overlay {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
z-index: 999;
}
</style>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<button id="confirm-btn">Delete Item</button>
<div class="overlay"></div>
<div class="confirm-dialog">
<p>Are you sure you want to delete this item?</p>
<button id="confirm-yes">Yes</button>
<button id="confirm-no">No</button>
</div>
<script>
$(document).ready(function() {
$('#confirm-btn').click(function() {
$('.overlay, .confirm-dialog').show();
});
$('#confirm-yes').click(function() {
alert('Item deleted!');
$('.overlay, .confirm-dialog').hide();
});
$('#confirm-no').click(function() {
$('.overlay, .confirm-dialog').hide();
});
});
</script>
</body>
</html>
display
属性。$(document).ready()
确保 DOM 完全加载后再绑定事件。通过以上方法,可以有效地解决在使用 jQuery 弹出确认对话框时可能遇到的问题。
没有搜到相关的文章