Jquery中使用SweetAlert使弹窗更漂亮

短命女 2023-10-18 19:21 173阅读 0赞

场景

一个漂亮的、响应性强、可定制的、可访问的JAVASCRIPT弹出框的替代。

效果

![Image 1][]watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0JBREFPX0xJVU1BTkdfUUlaSEk_size_16_color_FFFFFF_t_70

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0JBREFPX0xJVU1BTkdfUUlaSEk_size_16_color_FFFFFF_t_70 1

SweetAlert官网:

https://sweetalert.bootcss.com/

Github:

https://github.com/t4t5/sweetalert

SweetAlert2:

https://sweetalert2.github.io/

实现

引入资源文件

  1. <script src=" href='https://cdn.bootcss.com/sweetalert/1.1.3/sweetalert.min.js">https://cdn.bootcss.com/sweetalert/1.1.3/sweetalert.min.js"></script>

或者将其下载手动在html中引用。

js文件下载:

https://download.csdn.net/download/badao_liumang_qizhi/11225516

具体参照官网文档。

在js中使用

创建一个简单的错误提示

  1. swal({
  2. type: 'error',
  3. title: '错误提示',
  4. text: '请选择一个架构!'
  5. })

监听两个按钮的事件以及回调函数

  1. swal({
  2. title: '创建新的架构?',
  3. text: '请您首先选择需要创建的架构等级!',
  4. type: 'warning',
  5. showCloseButton: true,
  6. showCancelButton: true,
  7. confirmButtonColor: "#DD6B55",
  8. cancelButtonColor: '#DD4B55',
  9. confirmButtonText: '下级创建',
  10. cancelButtonText: '同级创建',
  11. }).then(function(isConfirm) {
  12. if (isConfirm.value === true) {
  13. var url = '/sysEnterpriseOrg/orgAdd.do';
  14. var data = {"id":sel,"op":"nextLevel"};
  15. $('#addDiv').load(url, data, function () {
  16. initOrgAddFormValidate("nextLevel");
  17. $("#orgAddModel").modal('show');
  18. });
  19. }
  20. if (isConfirm.dismiss === "cancel") {
  21. var url = '/sysEnterpriseOrg/orgAdd.do';
  22. var data = {"id":sel,"op":"sameLevel"};
  23. $('#addDiv').load(url, data, function () {
  24. initOrgAddFormValidate("sameLevel");
  25. $("#orgAddModel").modal('show');
  26. });
  27. }
  28. });

上面具体含义比较简单,见文知意,具体参照官方文档。

完整示例代码:

  1. /**
  2. * 添加架构
  3. * @returns {boolean}
  4. */
  5. function orgAdd() {
  6. var ref = $('#org_tree').jstree(true),
  7. sel = ref.get_selected();
  8. if(!sel.length) {
  9. swal({
  10. type: 'error',
  11. title: '错误提示',
  12. text: '请选择一个架构!'
  13. })
  14. return false;
  15. }
  16. sel = sel[0];
  17. swal({
  18. title: '创建新的架构?',
  19. text: '请您首先选择需要创建的架构等级!',
  20. type: 'warning',
  21. showCloseButton: true,
  22. showCancelButton: true,
  23. confirmButtonColor: "#DD6B55",
  24. cancelButtonColor: '#DD4B55',
  25. confirmButtonText: '下级创建',
  26. cancelButtonText: '同级创建',
  27. }).then(function(isConfirm) {
  28. if (isConfirm.value === true) {
  29. var url = '/sysEnterpriseOrg/orgAdd.do';
  30. var data = {"id":sel,"op":"nextLevel"};
  31. $('#addDiv').load(url, data, function () {
  32. initOrgAddFormValidate("nextLevel");
  33. $("#orgAddModel").modal('show');
  34. });
  35. }
  36. if (isConfirm.dismiss === "cancel") {
  37. var url = '/sysEnterpriseOrg/orgAdd.do';
  38. var data = {"id":sel,"op":"sameLevel"};
  39. $('#addDiv').load(url, data, function () {
  40. initOrgAddFormValidate("sameLevel");
  41. $("#orgAddModel").modal('show');
  42. });
  43. }
  44. });
  45. }

效果:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0JBREFPX0xJVU1BTkdfUUlaSEk_size_16_color_FFFFFF_t_70 2

[Image 1]:

发表评论

表情:
评论列表 (有 0 条评论,173人围观)

还没有评论,来说两句吧...

相关阅读

    相关 JQuery实现的效果

    这是笔者实际项目中的一个需求,我们先来看看特效。 ![这里写图片描述][20150619162138786] 页面加载时弹出窗口,点击关闭按钮,窗口消失并呈现动画效果。