jQuery 是一个快速、小巧且功能丰富的 JavaScript 库,它简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互。jQuery 确认提示框通常用于在用户执行某些操作前,提示用户确认其操作意图。
jQuery 确认提示框通常有以下几种类型:
jQuery UI Dialog
或 SweetAlert
。确认提示框常用于以下场景:
以下是一个使用 jQuery 和 SweetAlert 创建确认提示框的示例:
<!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 Example</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert@2.1.2/dist/sweetalert.min.js"></script>
</head>
<body>
<button id="deleteButton">Delete Item</button>
<script>
$(document).ready(function() {
$('#deleteButton').click(function() {
swal({
title: "Are you sure?",
text: "You will not be able to recover this item!",
icon: "warning",
buttons: true,
dangerMode: true,
})
.then((willDelete) => {
if (willDelete) {
swal("Deleted!", "Your item has been deleted.", "success");
// 在这里执行删除操作
} else {
swal("Cancelled", "Your item is safe :)", "error");
}
});
});
});
</script>
</body>
</html>
通过以上方法,可以有效地使用 jQuery 和 SweetAlert 创建和处理确认提示框。