xwzPlugin---confirmAndAlert

本文介绍了一个基于jQuery的自定义弹窗插件实现,包括警告框与确认框的功能。该插件允许用户自定义标题、消息内容及回调函数,并通过简单的API调用即可在页面上展示弹窗。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

作者:LoveEmperor

1.js插件代码

(function($) {
// xwzPlugin---confirm
   $.alerts = {
      alert: function(title, message, callback) {
         if( title == null ) title = 'Alert';
         $.alerts._show(title, message, null, 'alert', function(result) {
            if( callback ) callback(result);
         });
      },

      confirm: function(title, message, callback) {
         if( title == null ) title = 'Confirm';
         $.alerts._show(title, message, null, 'confirm', function(result) {
            if( callback ) callback(result);
         });
      },


      _show: function(title, msg, value, type, callback) {

         var _html = "";

         _html += '<div id="xwz_box"></div><div id="xwz_con"><span id="xwz_tit">' + title + '</span>';
         _html += '<div id="xwz_msg">' + msg + '</div><div id="xwz_btnbox">';
         if (type == "alert") {
            _html += '<input id="xwz_btn_ok1" type="button" value="确定" />';
         }
         if (type == "confirm") {
            _html += '<input id="xwz_btn_ok" type="button" value="确定" />';
            _html += '<input id="xwz_btn_no" type="button" value="取消" />';
         }
         _html += '</div></div>';

         //必须先将_html添加到body,再设置Css样式
         $("body").append(_html); GenerateCss();

         switch( type ) {
            case 'alert':

               $("#xwz_btn_ok1").click( function() {
                  $.alerts._hide();
                  callback(true);
               });
               $("#xwz_btn_ok1").focus().keypress( function(e) {
                  if( e.keyCode == 13 || e.keyCode == 27 ) $("#xwz_btn_ok").trigger('click');
               });
               break;
            case 'confirm':

               $("#xwz_btn_ok").click( function() {
                  $.alerts._hide();
                  if( callback ) callback(true);
               });
               $("#xwz_btn_no").click( function() {
                  $.alerts._hide();
                  if( callback ) callback(false);
               });
               $("#xwz_btn_no").focus();
               $("#xwz_btn_ok, #xwz_btn_no").keypress( function(e) {
                  if( e.keyCode == 13 ) $("#xwz_btn_ok").trigger('click');
                  if( e.keyCode == 27 ) $("#xwz_btn_no").trigger('click');
               });
               break;


         }
      },
      _hide: function() {
         $("#xwz_box,#xwz_con").remove();
      }
   }
   // Shortuct functions
   xwzalert = function(title, message, callback) {
      $.alerts.alert(title, message, callback);
   }

   xwzconfirm = function(title, message, callback) {
      $.alerts.confirm(title, message, callback);
   };


   //生成Css
   var GenerateCss = function () {

      $("#xwz_box").css({ width: '100%', height: '100%', zIndex: '99999', position: 'fixed',
         filter: 'Alpha(opacity=60)', backgroundColor: 'black', top: '0', left: '0', opacity: '0.6'
      });

      $("#xwz_con").css({ zIndex: '999999', width: '55%', position: 'fixed',
         backgroundColor: 'White'
      });

      $("#xwz_tit").css({ display: 'block', fontSize: '14px', color: 'white', padding: '10px 15px',
         backgroundColor: 'rgb(56,126,245)',
         fontWeight: 'bold'
      // borderBottom: '3px solid #009BFE',
      });

      $("#xwz_msg").css({ padding: '20px', lineHeight: '20px',
          fontSize: '13px'
      // borderBottom: '1px dashed #DDD',
      });

      $("#xwz_ico").css({ display: 'block', position: 'absolute', right: '10px', top: '9px',
         border: '1px solid Gray', width: '18px', height: '18px', textAlign: 'center',
         lineHeight: '16px', cursor: 'pointer',  fontFamily: '微软雅黑'
      });

      $("#xwz_btnbox").css({  textAlign: 'center' });
      $("#xwz_btn_ok1").css({width: '100%', height: '35px', color: 'white', border: 'none',backgroundColor: "rgba(56,126,245,0.9)" });
      $("#xwz_btn_ok,#xwz_btn_no").css({ width: '50%', height: '30px', color: 'white', border: 'none' });
      $("#xwz_btn_ok").css({ backgroundColor: "rgba(56,126,245,0.9)" });
      $("#xwz_btn_no").css({ backgroundColor: "rgba(50,205,94,0.9)"});
      // true #387EF5
      // false rgba(50,205,94,0.79)

      //右上角关闭按钮hover样式
      $("#xwz_ico").hover(function () {
         $(this).css({ backgroundColor: 'Red', color: 'White' });
      }, function () {
         $(this).css({ backgroundColor: '#DDD', color: 'black' });
      });

      var _widht = document.documentElement.clientWidth; //屏幕宽
      var _height = document.documentElement.clientHeight; //屏幕高

      var boxWidth = $("#xwz_con").width();
      var boxHeight = $("#xwz_con").height();

      //让提示框居中
      $("#xwz_con").css({ top: (_height - boxHeight) / 2 + "px", left: (_widht - boxWidth) / 2 + "px" });
   }


})(jQuery);
2.页面调用:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   <meta name="viewport"
        content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width" />
   <title>jq-each遍历</title>
   <script type="text/javascript" src="../resources/js/bin/jquery.min.js"></script>
</head>
<body>
<div id="title"></div>

<input id="oneAlert" type="button" value="支付" />
<input id="wxpay" type="button" value="支付" />

</body>
<script type="text/javascript" src="../resources/js/myConfirm.js"></script>
<script language="javascript" type="text/javascript">

// window.confirm("ddd");
   var data = {"list":[{"id":1,"content":"人生在世"},{"id":2,"content":"哪能没曲折"}]};
   var str1 = '';
   var str2 = '';
   $.each(data.list, function(i, item) {

      console.log(item.id);

      str1 += '<p>'+data.list[i].id+data.list[i].content+'</p>';

      $("#title").html(str1)
   });

   //-------------------------
   $("#wxpay").bind("click", function () {

      xwzconfirm('微信支付','请确认微信支付是否完成',function(r){
         if(r)
         {
            //确定
         }
      });
   });

   $("#oneAlert").bind('click',function () {
      xwzalert('微信支付','请确认微信支付是否完成',function(r){
         if(r)
         {
            //确定
         }
      });
   })

</script>
</html>
3.效果图




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王子様~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值