2011-02-06 28 views
4

我想以我自己的方式替換原生JavaScript alert(),這樣我就能夠控制主題並使其具有更多JQueryUI的外觀和感覺。 我試過很多選擇--JQueryUI對話框,jAlert,jqAlert。然而,它們似乎並不像原始警報那樣以同樣的方式同步運行。任何JavaScript本地的JQuery alert()替代?

實施例:

function mytest() 
{ 
    alert('one'); 
    alert('two'); 
    alert('three'); 
} 

在本例中,與原來的警報()時,3個對話框將顯示一個接一個地在一行之後。但在替代品中,它們一下子出現!

有什麼想法?

+3

剛剛鏈使用回調函數的jQuery UI對話框。 – 2011-02-06 13:37:07

回答

13

原生alert()使瀏覽器停止了一個你不會發現,做任何第三方庫,因爲這是不可能的。*


編輯

我扔在一起的,你如何使用一個jQuery的對話,而不是警報的快速演示。

var alertManager = (function() { 
    var _queue = [], 
     _opts = { 
      modal: true, 
      autoOpen: false, 
      buttons: { 
       OK: function() 
       { 
        $(this).dialog('close'); 
        var next = _queue.shift(); 
        if (typeof next === 'string') 
        { 
         _dialog.text(next).dialog('open'); 
        } 
       } 
      } 
     }, 
     _dialog = $('<div id="alertDialog" title="Alert!"></div>').dialog(_opts), 

     _self = {}; 

    _self.show = function (message) { 
     if (_dialog.dialog('isOpen')) { 
      _queue.push(String(message)); 
     } 
     else { 
      _dialog.text(message).dialog('open'); 
     } 
    } 

    return _self; 
}()); 



$('#clicky').click(function() 
{ 
    alertManager.show('alert numero uno'); 
    alertManager.show('alert #2'); 
    alertManager.show({foo: 'bar'}); 
    alertManager.show(document.getElementById('clicky')); 
    alertManager.show('last one'); 
}); 

Hot demo action over here →

你也可以把它變成一個jQuery插件很容易地。


*雖然你可以用while循環,旋轉,而對話框打開假的吧。我不要推薦這個。

+0

這更好留下來作爲(厚顏無恥)評論。 OP可能不想讓瀏覽器停止,而是讓對話框依次顯示。 – ash 2011-02-06 13:42:19

+2

@Jasie以及OP要求的東西的功能「像原始警報一樣的方式」:同步。因此,OP幾乎肯定會**想讓瀏覽器停止。 – Pointy 2011-02-06 13:52:20

+0

我已經嘗試while循環,它會導致瀏覽器完全停止不能看到對話框,更不用說瀏覽器發出停止無響應腳本的警告。 – 2011-02-06 14:03:14

0

您可以輕鬆地模仿使用jQueryUI對話框的常規js警報的同步性。

<div id="dialog" title="Dialog Title">Dialog</div> 
<div id="dialog2" title="Dialog Title">Another dialog</div> 
$("#dialog").dialog({ 
    close: function(event, ui) { 
     $("#dialog2").dialog(); 
    } 
}); 

現在,當您關閉第一個對話框,第二個打開:當您關閉對話框,在這種情況下,close,被稱爲 - 只需使用提供的事件。

2

一個jQuery警告:使用

JQuery.fn.alert = function(message) { 
    alert(message); 
    }; 

例如:

$("#item1").alert("hello"); 

噢,我的上帝:d

jQuery的只是一個DOM框架。這不是其他的JavaScript! jquery只是一些javascript行。而不是取代javascript。

如果你想創建一個對話框,那麼我可以建議你搜索jquery插件。

http://choosedaily.com/1178/15-jquery-popup-modal-dialog-plugins-tutorials/

0

如何分層警報?
它們仍然會一次出現,但用戶只能看到第一個,直到她關閉它,然後第二個出現(顯示)等等。
- 可以很容易地通過更新的「全球」最後的z-index變量acheaved ..

0

在平均時間我最近還創建了一個新的功能,允許確認盒用jqueryui對話框。

這是非常容易使用

dlgConfirm('Are you sure you want to cancel this change-email request? <br><label>Current password<br><input type="password"></label>', 
      'Cancel Email Change', 'Continue', function(dlg){ 
     //do ajax or something 
     return false; //do not close dialog until ajax is complete 
    } 

dlgConfirm('Are you sure you want to submit this form', function(dlg){ 
    $('form', dlg).submit(); 
    return true; 
}); 

這裏是源代碼(公共領域):

<script> 
    /** 
    * Open confirmation dialog (jqueryui modal) 
    * 
    * @param {string} c_text text/html to show in the dialog box 
    * @param {string|function(dlg_element)} c_title|confirm_callback title of the dialog box (or callback function) 
    * @param {string|function(dlg_element)} c_btn_text|confirm_callback confirm button text (or callback function) 
    * @param {string|function(dlg_element)} c_btn_cancel_text|confirm_callback cancel button text (defaults to 'Cancel') (or callback function) 
    * @param {function(dlg_element)} confirm_callback callback after the modal box is confirmed 
    */ 
    function dlgConfirm(c_text, c_title, c_btn_text, c_btn_cancel_text, confirm_callback){ 

     if (typeof(confirm_callback) !== 'function'){ 
     if (typeof(c_title) == 'function'){ 
      confirm_callback = c_title; 
     }else if (typeof(c_btn_text) == 'function'){ 
      confirm_callback = c_btn_text; 
     }else if (typeof(c_btn_cancel_text) == 'function'){ 
      confirm_callback = c_btn_cancel_text; 
     } 
     } 

     if (typeof(c_text) !== 'string'){ 
     c_text = 'Are you sure?'; 
     } 
     if (typeof(c_title) !== 'string'){ 
     c_title = 'Confirm'; 
     } 
     if (typeof(c_btn_text) !== 'string'){ 
     c_btn_text = 'Confirm'; 
     } 
     if (typeof(c_btn_cancel_text) !== 'string'){ 
     c_btn_cancel_text = 'Cancel'; 
     }  

     if ($('#dlgConfirm').length == 0){ 
     $('body').append('<div id="dlgConfirm" title="Confirm" style="display: none">Are you sure?</div>'); 
     } 
     var btns = {}; 
     btns[c_btn_text] = function() { 
      var dlg = this; 
      if (typeof(confirm_callback) == 'function'){ 
       if (confirm_callback(dlg) !== false){ 
        $(this).dialog('close'); 
       } 
      }   
     }; 
     btns[c_btn_cancel_text] = function() { 
      $(this).dialog("close"); 
     }; 

     $('#dlgConfirm').dialog({ 
     title: c_title, 
     autoOpen: false, 
     resizable: false, 
     //height:170, //commented out so autosize works 
     modal: true, 
     buttons: btns 
     }).html(c_text).dialog('open'); 
    } 
    </script>