JS~重写alter与confirm,让它们变成fancybox风格

插件与系统命令

对于很多JS弹框插件来说,都提供了alter,confirm等功能,如fancybox,Boxy等插件,今天来介绍一下如何将系统的alter和confirm替换成指定插件的alter和confirm,使用替换的方式的好处就是不用修改之前的代码,这在面向对象里,

叫做“对修改关闭,对扩展开放”,也称为OCP原则,即开闭原则。

fancybox替换系统命令,它需要有一个HTML容器来展现弹框

<script id="MessageBox_delete" type="text/html">
<!--试题删除确认弹出框 start-->
<div class="r_warnBox" style="width: 276px;">
<div class="r_warnBox_tit">
<h3><strong>提示</strong></h3>
</div>
<div class="r_roomBoxContDel">
<p class="alert">您确定删除作业吗?</p>
<p class="confirm"><a class="sure" href="javascript:;">确定</a><a class="cancel" href="javascript:;" onclick="$.fancybox.close();">取消</a></p>
</div>
<a class="r_warnBox_close" href="javascript:;" onclick="$.fancybox.close();"></a>
</div>
<!--试题删除确认弹出框 end-->
</script>
<!--弹出框1 start-->
<script id="MessageBox_message" type="text/html">
<div class='r_warnBox' style='width: 433px; padding-right: 30px;'>
<div class='r_warnBox_tit'>
<h3><strong>提示
</strong></h3>
</div>
<div id='alert_message' class='r_roomBoxCont'>
<p class='alert'>[Content]</p>
</div>
<a class='r_warnBox_close' href='javascript:;' onclick="$.fancybox.close()"></a>
</div>
</script>
<!--弹出框1 end-->

下面是复写语句

 window.alert = function (msg) {
var reg = new RegExp("\\[([^\\[\\]]*?)\\]", 'igm'); //i g m是指分别用于指定区分大小写的匹配、全局匹配和多行匹配。
var html = document.getElementById("MessageBox_message").innerHTML;
var result = html.replace(reg, function (node, key) { return { 'Content': msg }[key]; });
$.fancybox(result, {
'padding': 0,
'scrolling': 'visible',
'closeBtn': false,
'modal': false
}); }
window.confirm = function (msg, callBack) {
$.fancybox($("#MessageBox_delete").html(), {
'padding': 0,
'scrolling': 'visible',
'closeBtn': false,
'modal': false,
afterShow: function () {
$('p.confirm a.sure').on('click', function () {
callBack();
});
}
}); }

下面是调用语句

function alerttest() {
alert("ok");
}
function confirmtest() {
confirm("are you sure!", function () { alert("删除成功") });
}

Boxy替换系统命令,使用相对简单,它已经为我们创建的弹框的DIV

window.alert = function (msg) {
Boxy.alert(msg);
} window.alert = function (msg,callback) {
Boxy.confirm(msg,callback);
}

调用和fancybox也是一样的

  function alerttest() {
alert("ok");
}
function confirmtest() {
confirm("are you sure!", function () { alert("删除成功") });
}

最后运行效果如图

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAATwAAACMCAIAAABu2oKQAAAN1ElEQVR4nO2ca3MT1x2H813yKVICJUTGF2iZhjSFJJ00TORMO2maTNx2nDTpi3bTJhgTLgHjGwoGGxvTGEIGb8LEJUCb2tiWJdmWLCPhK/JNiuQ7CJ++ONrds6vd1cWWlmP/nvnNIK+OtLvSefQ/56zEU76ZZQRBOMpTlh8BgiAZBdIiyIalP7T4b3/k847Rshbf6/Weg9V9L9e4Sh2eD9v853+Y/OF+bEP2AmkRZGNyzT37XrOvoPLuM590bqu4u/1oz47PenYc7dle2f2Tw3e3fdK5/1TvJ9eDdwLRde4I0iLIetM3ufCP64HnK+9uO3L3uRPOgtOuwmpPUY2nuKa/uKa/qMZTeMZtO+Xaeaz3mU+79p/qbep6sJ7dQVoEWVe6x+bfafI+82nXrhPOwjPuktr+PfUDySmpGyiu7d9d5f7psd6dR7o+7xjNeo+QFkGyj+vBwttNg88e6S447TLSVZOiGs+uk33bD3dVfT+e3U4hLYJkGe/00sfXg88e6d5d5S6pS8tYmuLa/uc/79t9rPsr12wW+4W0CJJlrrpmCo/3FJx2sca+2OB9scFrpOtbbYEXznmpt7tOOA990d87Pp/pfjOQNv0PEgTZ9Cmp7d9+pHvXSSc7Kn77SsA5udDSN/tyoy/5IRedMwNTS//sGKd/FlV7th/tee6kE9IiSD5ScNq142hPYbWH3eh+sEgIWVsjN/w//uKLQXn73vqBpt4ZQgghZCK6SjeW1A3YTrl2HO1xPViAtAiS8zx30rnrhFOz+PQXceQ/92OEkNX4WpNz5sAF3576gb31A819CWOD4ZXjt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