jQuery 是一个快速、小巧且功能丰富的 JavaScript 库,它简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互。jQuery 确认对话框通常是通过 jQuery UI 或其他插件实现的,用于在用户执行某些操作前进行确认。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery UI Confirm Dialog</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
</head>
<body>
<button id="confirmButton">Delete Item</button>
<script>
$(function() {
$("#confirmButton").click(function() {
if (confirm("Are you sure you want to delete this item?")) {
alert("Item deleted!");
} else {
alert("Delete cancelled.");
}
});
});
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Custom Confirm Dialog</title>
<style>
#customDialog {
display: none;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: white;
padding: 20px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
}
</style>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<button id="confirmButton">Delete Item</button>
<div id="customDialog">
<p>Are you sure you want to delete this item?</p>
<button id="confirmYes">Yes</button>
<button id="confirmNo">No</button>
</div>
<script>
$(function() {
$("#confirmButton").click(function() {
$("#customDialog").show();
});
$("#confirmYes").click(function() {
$("#customDialog").hide();
alert("Item deleted!");
});
$("#confirmNo").click(function() {
$("#customDialog").hide();
alert("Delete cancelled.");
});
});
</script>
</body>
</html>
原因:
解决方法:
$(document).ready(function() {
$("#confirmButton").click(function() {
$("#customDialog").show();
});
});
通过以上方法,可以确保确认对话框能够正常显示。
领取专属 10元无门槛券
手把手带您无忧上云