您好,欢迎访问一九零五行业门户网

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

更改框体主要用到的是更改系统的内置控件winpop下面是winpop具体代码
(function(window, jquery, undefined) { var htmls = { ovl: '
' + '' + '
' + '
' + '
', alert: '', confirm: '' + '' } 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不可少

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

推荐信息