js 重寫 bootstrap 樣式 alert/confirm 訊息視窗

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   ar   2014   art   

相信很多人都受夠了 alert、confirm 的樣子,最近正在用 bootstrap 做項目,順便封裝了一個 bootstrap 樣式的訊息框。

實現起來很簡單,bootstrap 本身就內建了 modal 模態框,樣子還不錯 :)就把它封裝一下用吧。

無碼無真相,少說多做,上代碼。

項目是Asp.net Mvc架構的,方便全域調用,我直接在全域 Layout 頁面加上以下HTML:

    <!-- system modal start -->        <div id="ycf-alert" class="modal">            <div class="modal-dialog modal-sm">                <div class="modal-content">                    <div class="modal-header">                        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>                        <h5 class="modal-title"><i class="fa fa-exclamation-circle"></i> [Title]</h5>                    </div>                    <div class="modal-body small">                        <p>[Message]</p>                    </div>                    <div class="modal-footer" >                        <button type="button" class="btn btn-primary ok" data-dismiss="modal">[BtnOk]</button>                        <button type="button" class="btn btn-default cancel" data-dismiss="modal">[BtnCancel]</button>                    </div>                </div>            </div>        </div>    <!-- system modal end -->

不多說了,都是bootstrap modal的樣式,不熟悉的朋友可以查查 bootstrap css ,中括弧[....]的內容會最終替換為我們傳入的參數顯示。

js 封裝如下:

;$(function () {    window.Modal = function () {        var reg = new RegExp("\\[([^\\[\\]]*?)\\]", ‘igm‘);        var alr = $("#ycf-alert");        var ahtml = alr.html();        //關閉時恢複 modal html 原樣,供下次調用時 replace 用        //var _init = function () {        //    alr.on("hidden.bs.modal", function (e) {        //        $(this).html(ahtml);        //    });        //}();        /* html 複原不在 _init() 裡面做了,重複調用時會有問題,直接在 _alert/_confirm 裡面做 */        var _alert = function (options) {            alr.html(ahtml);    // 複原            alr.find(‘.ok‘).removeClass(‘btn-success‘).addClass(‘btn-primary‘);            alr.find(‘.cancel‘).hide();            _dialog(options);            return {                on: function (callback) {                    if (callback && callback instanceof Function) {                        alr.find(‘.ok‘).click(function () { callback(true) });                    }                }            };        };        var _confirm = function (options) {            alr.html(ahtml); // 複原            alr.find(‘.ok‘).removeClass(‘btn-primary‘).addClass(‘btn-success‘);            alr.find(‘.cancel‘).show();            _dialog(options);            return {                on: function (callback) {                    if (callback && callback instanceof Function) {                        alr.find(‘.ok‘).click(function () { callback(true) });                        alr.find(‘.cancel‘).click(function () { callback(false) });                    }                }            };        };        var _dialog = function (options) {            var ops = {                msg: "提示內容",                title: "操作提示",                btnok: "確定",                btncl: "取消"            };            $.extend(ops, options);            console.log(alr);            var html = alr.html().replace(reg, function (node, key) {                return {                    Title: ops.title,                    Message: ops.msg,                    BtnOk: ops.btnok,                    BtnCancel: ops.btncl                }[key];            });                        alr.html(html);            alr.modal({                width: 500,                backdrop: ‘static‘            });        }        return {            alert: _alert,            confirm: _confirm        }    }();});

 

調用方法:

// 四個選項都是選擇性參數Modal.alert(    {        msg: ‘內容‘,        title: ‘標題‘,        btnok: ‘確定‘,        btncl:‘取消‘    });// 如需增加回呼函數,後面直接加 .on( function(e){} );// 點擊“確定” e: true// 點擊“取消” e: falseModal.confirm(    {        msg: "是否刪除角色?"    })    .on( function (e) {        alert("返回結果:" + e);    });

:

還有一些細節需要完善,譬如blockUI等,晚了,先睡。

有空再補全。

js 重寫 bootstrap 樣式 alert/confirm 訊息視窗

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.