使用jQuery实现更改默认alert框体


Posted in Javascript onApril 13, 2015

更改框体主要用到的是更改系统的内置控件winpop下面是winpop具体代码

(function(window, jQuery, undefined) {
 
   var HTMLS = {
     ovl: '<div class="J_WinpopMask winpop-mask" id="J_WinpopMask"></div>' + '<div class="J_WinpopBox winpop-box" id="J_WinpopBox">' + '<div class="J_WinpopMain winpop-main"></div>' + '<div class="J_WinpopBtns winpop-btns"></div>' + '</div>',
     alert: '<input type="button" class="J_AltBtn pop-btn alert-button" value="确定">',
     confirm: '<input type="button" class="J_CfmFalse pop-btn confirm-false" value="取消">' + '<input type="button" class="J_CfmTrue pop-btn confirm-true" value="确定">'
   }
 
   function Winpop() {
     var config = {};
     this.get = function(n) {
       return config[n];
     }
 
     this.set = function(n, v) {
       config[n] = v;
     }
     this.init();
   }
 
   Winpop.prototype = {
     init: function() {
       this.createDom();
       this.bindEvent();
     },
     createDom: function() {
       var body = jQuery("body"),
         ovl = jQuery("#J_WinpopBox");
 
       if (ovl.length === 0) {
         body.append(HTMLS.ovl);
       }
 
       this.set("ovl", jQuery("#J_WinpopBox"));
       this.set("mask", jQuery("#J_WinpopMask"));
     },
     bindEvent: function() {
       var _this = this,
         ovl = _this.get("ovl"),
         mask = _this.get("mask");
       ovl.on("click", ".J_AltBtn", function(e) {
         _this.hide();
       });
       ovl.on("click", ".J_CfmTrue", function(e) {
         var cb = _this.get("confirmBack");
         _this.hide();
         cb && cb(true);
       });
       ovl.on("click", ".J_CfmFalse", function(e) {
         var cb = _this.get("confirmBack");
         _this.hide();
         cb && cb(false);
       });
       mask.on("click", function(e) {
         _this.hide();
       });
       jQuery(document).on("keyup", function(e) {
         var kc = e.keyCode,
           cb = _this.get("confirmBack");;
         if (kc === 27) {
           _this.hide();
         } else if (kc === 13) {
           _this.hide();
           if (_this.get("type") === "confirm") {
             cb && cb(true);
           }
         }
       });
     },
     alert: function(str, btnstr) {
       var str = typeof str === 'string' ? str : str.toString(),
         ovl = this.get("ovl");
       this.set("type", "alert");
       ovl.find(".J_WinpopMain").html(str);
       if (typeof btnstr == "undefined") {
         ovl.find(".J_WinpopBtns").html(HTMLS.alert);
       } else {
         ovl.find(".J_WinpopBtns").html(btnstr);
       }
       this.show();
     },
     confirm: function(str, callback) {
       var str = typeof str === 'string' ? str : str.toString(),
         ovl = this.get("ovl");
       this.set("type", "confirm");
       ovl.find(".J_WinpopMain").html(str);
       ovl.find(".J_WinpopBtns").html(HTMLS.confirm);
       this.set("confirmBack", (callback || function() {}));
       this.show();
     },
     show: function() {
       this.get("ovl").show();
       this.get("mask").show();
     },
     hide: function() {
       var ovl = this.get("ovl");
       ovl.find(".J_WinpopMain").html("");
       ovl.find(".J_WinpopBtns").html("");
       ovl.hide();
       this.get("mask").hide();
     },
     destory: function() {
       this.get("ovl").remove();
       this.get("mask").remove();
       delete window.alert;
       delete window.confirm;
     }
   };
 
   var obj = new Winpop();
   window.alert = function(str) {
     obj.alert.call(obj, str);
   };
   window.confirm = function(str, cb) {
     obj.confirm.call(obj, str, cb);
   };
 })(window, jQuery);

然后实例化对象

var obj = new Winpop(); // 创建一个Winpop的实例对象
 // 覆盖alert控件
 window.alert = function(str) {
   obj.alert.call(obj, str);
 };
 // 覆盖confirm控件
 window.confirm = function(str, cb) {
   obj.confirm.call(obj, str, cb);
 };

以下JS不可少

<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="winpop.js"></script>

以上所述就是本文的全部内容了,希望对大家能够有所帮助。

Javascript 相关文章推荐
List Installed Software Features
Jun 11 Javascript
javascript 表单验证常见正则
Sep 28 Javascript
原生js ActiveXObject获取execl里面的值
Nov 01 Javascript
浅谈JS日期(Date)处理函数
Dec 07 Javascript
jquery单击事件和双击事件冲突解决方案
Mar 02 Javascript
第十章之巨幕页头缩略图与警告框组件
Apr 25 Javascript
微信小程序 出现错误:{&quot;baseresponse&quot;:{&quot;errcode&quot;:-80002,&quot;errmsg&quot;:&quot;&quot;}}解决办法
Feb 23 Javascript
vue组件间通信解析
Mar 01 Javascript
php简单数据库操作类的封装
Jun 08 Javascript
JS中的回调函数实例浅析
Mar 21 Javascript
js的Object.assign用法示例分析
Mar 05 Javascript
Vue自定义表单内容检查rules实例
Oct 30 Javascript
javascript异步处理工作机制详解
Apr 13 #Javascript
JavaScript中DOM详解
Apr 13 #Javascript
js 获取元素在页面上的偏移量的方法汇总
Apr 13 #Javascript
javascript中scrollTop详解
Apr 13 #Javascript
jQuery实现的在线答题功能
Apr 12 #Javascript
jQuery插件bxSlider实现响应式焦点图
Apr 12 #Javascript
jQuery插件Skippr实现焦点图幻灯片特效
Apr 12 #Javascript
You might like
php字符串截取中文截取2,单字节截取模式
2007/12/10 PHP
PHP实现转盘抽奖算法分享
2020/04/15 PHP
php实现微信小程序授权登录功能(实现流程)
2019/11/13 PHP
javascript:void(0)的真正含义实例分析
2008/08/20 Javascript
使用原生javascript创建通用表单验证——更锋利的使用dom对象
2011/09/13 Javascript
Javascript验证上传图片大小[前台处理]
2014/07/18 Javascript
JavaScript设计模式之工厂模式和构造器模式
2015/02/11 Javascript
jquery获取css的color值返回RGB的方法
2015/12/18 Javascript
BootStrap的alert提示框的关闭后再显示怎么解决
2016/05/17 Javascript
AngularJS使用ng-repeat指令实现下拉框
2016/08/23 Javascript
Bootstrap3 图片(响应式图片&amp;图片形状)
2017/01/04 Javascript
JS回调函数简单用法示例
2017/02/09 Javascript
基于angular实现模拟微信小程序swiper组件
2017/06/11 Javascript
JS传播事件、取消事件默认行为、阻止事件传播详解
2017/08/14 Javascript
nodejs图片处理工具gm用法小结
2018/12/12 NodeJs
vue按需加载实例详解
2019/09/06 Javascript
layui-tree实现Ajax异步请求后动态添加节点的方法
2019/09/23 Javascript
js实现淘宝首页的banner栏效果
2019/11/26 Javascript
python 将字符串转换成字典dict
2013/03/24 Python
分析Python编程时利用wxPython来支持多线程的方法
2015/04/07 Python
Python中字典创建、遍历、添加等实用操作技巧合集
2015/06/02 Python
python字符串,数值计算
2016/10/05 Python
使用Python爬了4400条淘宝商品数据,竟发现了这些“潜规则”
2018/03/23 Python
python实现百度语音识别api
2018/04/10 Python
使用python和pygame制作挡板弹球游戏
2019/12/03 Python
Python基于codecs模块实现文件读写案例解析
2020/05/11 Python
HTML5之web workers_动力节点Java学院整理
2017/07/17 HTML / CSS
HTML5 声明兼容IE的写法
2011/05/16 HTML / CSS
英国虚拟主机服务商:eUKhost
2016/08/16 全球购物
汽车专业人才自我鉴定范文
2013/12/29 职场文书
班组安全员工作职责
2014/02/01 职场文书
简历的自我评价范文
2014/02/04 职场文书
干部外出学习心得体会
2016/01/18 职场文书
go原生库的中bytes.Buffer用法
2021/04/25 Golang
Node.js实现断点续传
2021/06/23 Javascript
MySQL常见优化方案汇总
2022/01/18 MySQL