js swal用法
Swal 是一个弹出框插件,可以用于在网页中显示提示信息、确认框、输入框等等。
使用 Swal 需要先引入它的 JavaScript 文件,可以通过以下方式引入:
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
然后可以在 JavaScript 代码中使用 Swal 的方法来创建弹出框。以下是一些常用的 Swal 方法:
- 弹出提示信息:
Swal.fire('Hello World');
- 弹出确认框:
Swal.fire({
title: 'Are you sure?',
text: 'You will not be able to recover this imaginary file!',
icon: 'warning',
showCancelButton: true,
confirmButtonText: 'Yes, delete it!',
cancelButtonText: 'No, cancel!',
reverseButtons: true
}).then((result) => {
if (result.isConfirmed) {
Swal.fire(
'Deleted!',
'Your imaginary file has been deleted.',
'success'
);
} else if (
result.dismiss === Swal.DismissReason.cancel
) {
Swal.fire(
'Cancelled',
'Your imaginary file is safe :)',
'error'
);
}
});
- 弹出输入框:
Swal.fire({
title: 'Enter your name',
input: 'text',
inputLabel: 'Name',
inputPlaceholder: 'Enter your name',
showCancelButton: true,
inputValidator: (value) => {
if (!value) {
return 'You need to enter your name!';
}
}
}).then((result) => {
if (result.isConfirmed) {
Swal.fire(`Hello ${result.value}!`);
}
});
这只是一些 Swal 的基本用法示例,Swal 还有更多功能和选项可以使用,你可以查看 Swal 的官方文档来了解更多信息
原文地址: https://www.cveoy.top/t/topic/iONq 著作权归作者所有。请勿转载和采集!