自定義瀏覽器alert ,拋棄掉死板的藍框 自定義風格提示框。jquey ui bootstrap 實現自定義 alert confirm prompt ,by大崔


首先看一下自定義提示框的效果圖

alert   普通的提示當然可以自定義樣式

confrim 確認框 支持callback  

 //message 提示的信息 ,callback(true/false)回調函數
 window.shconfirm = function (message, callback) 
回調函數參數為 true/false

prompt  邀請用戶輸入框

 //message 提示的信息 ,callback(msg)回調函數(用戶輸入的消息), param:regex 輸入的 正則驗證,regexmsg 正則驗證不通過的提示
 window.shprompt = function (message, callback, regex, regexmsg)
這里 message 為提示消息 *
callback 為回調函數 * 回傳參數為 用戶輸入的值(userinputmsg)
regex 和 regexmsg 這2個參數是 選填項 用於驗證用戶輸入,2個參數需要同時出現。不能單獨使用。

以下是js的實現,

當前這個是整合了 jquery ui 和 bootstrap 自己封裝的一個 alert 提示。

  1 (function () {
  2     var _shconfirm = {};
  3     var _shprompt = {};
  4     //閉包初始化;
  5     $(function () {
  6         $("#dialogalert").dialog({
  7             modal: true,
  8             autoOpen: false,
  9             show: {
 10                 effect: "blind",
 11                 duration: 500
 12             },
 13             hide: {
 14                 effect: "explode",
 15                 duration: 500
 16             },
 17             buttons: {
 18                 確定: function () {
 19                     $(this).dialog("close");
 20                 }
 21             }
 22         });
       
23 $("#dialogconfirm").dialog({ 24 modal: true, 25 autoOpen: false, 26 show: { 27 effect: "slide", 28 duration: 500 29 }, 30 hide: { 31 effect: "drop", 32 duration: 500 33 }, 34 buttons: { 35 確定: function () { 36 _shconfirm.shconfirmCallBack(true); 37 $(this).dialog("close"); 38 }, 39 40 取消: function () { 41 _shconfirm.shconfirmCallBack(false); 42 $(this).dialog("close"); 43 44 } 45 } 46 }); 47 $("#dialogprompt").dialog({ 48 modal: true, 49 autoOpen: false, 50 show: { 51 effect: "blind", 52 duration: 500 53 }, 54 hide: { 55 effect: "puff", 56 duration: 500 57 }, 58 buttons: { 59 確定: function () { 60 if (_shprompt.shpromptObj.regex) { 61 if (!_shprompt.shpromptObj.regex.test($("#dialogprompt .text").val())) { 62 $("#dialogprompt .alert .promptmsg").html(_shprompt.shpromptObj.regexmsg); 63 $("#dialogprompt .alert").slideDown(); 64 return; 65 } else { 66 $("#dialogprompt .alert").hide(); 67 } 68 } 69 _shprompt.shpromptObj.callback($("#dialogprompt .text").val()); 70 $(this).dialog("close"); 71 }, 72 73 取消: function () { 74 _shprompt.shpromptObj.callback($("#dialogprompt .text").val()); 75 $(this).dialog("close"); 76 77 } 78 } 79 }); 80 }); 81 82 window.shalert = function (message) { 83 $("#dialogalert .msgcontent").html(message); 84 $("#dialogalert").dialog("open"); 85 }; 86 //message 提示的信息 ,callback(true/false)回調函數 87 window.shconfirm = function (message, callback) { 88 $("#dialogconfirm .msgcontent").html(message); 89 $("#dialogconfirm").dialog("open"); 90 _shconfirm.shconfirmCallBack = callback; 91 }; 92 //message 提示的信息 ,callback(msg)回調函數(用戶輸入的消息), param:regex 輸入的 正則驗證,regexmsg 正則驗證不通過的提示 93 window.shprompt = function (message, callback, regex, regexmsg) { 94 $("#dialogprompt .msgcontent").html(message); 95 $("#dialogprompt").dialog("open"); 96 _shprompt.shpromptObj = { 97 callback: callback, 98 regex: regex, 99 regexmsg: regexmsg 100 }; 101 } 102 })();

 

以下是調用代碼 

confirm //比可惜的是 js沒法模擬 js腳本暫停 所以只能以回調函數的方式 來繼續下一步操作。

 function ShConfirm() {
            shconfirm("確定要這么做嗎!", function (result) {
                if (result) {
                    alert("點擊了確定");
                } else {
                    alert("點擊了取消");
                }
            });
        }
  function ShPrompt() {
            shprompt("請問1+1等於幾!", function (text) {
                alert("用戶輸入了:" + text);
            }, /^\d{1,}$/, "請輸入數字!");
        }

shalert 就直接用就行了。和 js的alert 效果一樣。

    <input type="button" name="name" value="ShAlert" onclick="shalert('保存成功!');" />
    <input type="button" name="name" value="ShConfirm" onclick="ShConfirm()" />
    <input type="button" name="name" value="ShPrompt" onclick="ShPrompt()" />

源碼我已經放在了 百度網盤上,歡迎大家學習交流。

源碼下載地址

http://pan.baidu.com/s/1c00Cl36

這個控件其實還有可重構的部分,比如初始化方法等等這些都沒有提取出來,因為任務緊所以先這么用着。

下一次優化時會處理這些問題。

 

 原版風格是這樣的,可以通過修改引用的css上實現 demo上有詳細說明。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM