覆盖javascript ALERT到jquery对话框

时间:2013-05-01 02:53:15

标签: javascript jquery jquery-ui

我试图做魔术并将我的所有平台javascript警报转到jquery对话框,我按照以下脚本

<div id="overrideAlert"></div>

<script>
window.alert = function(message) {
    $('#overrideAlert').text(message).dialog({
        modal:true,
        title:'Message',
        buttons: {
            'OK':function(){
                $(this).dialog('close');
            }
        }
    });
};
</script>

但没有运气。

这有一个干净的解决方案吗?谢谢,

4 个答案:

答案 0 :(得分:8)

我更喜欢动态div而不是

$('<div />').text(message).dialog({
    modal:true,
    title:'Message',
    buttons: {
        'OK':function(){
            $(this).dialog('close');
        }
    },
    close:function(){ $(this).dialog('destroy').remove(); }
});

DEMO.

答案 1 :(得分:3)

它只是有效。

检查 jsfiddle demo

注意:您无法直接在alert('foo');的{​​{1}}标记内拨打<head>,因为dom上的div元素尚未就绪。

答案 2 :(得分:0)

您的代码看起来很好,但请确保在页面中添加jquery和jquery-ui库。

演示:Plunker

答案 3 :(得分:-1)

如果我们在警报自动进行之前提交页面。 有些人在那里保存了成功的消息但没有要求&#34;确定&#34;。 我已经完成了警告。任何建议。 window.alert = function(message,fallback){

$(document.createElement('div')).attr({title: 'Alert', 'class': 'alert'}).html(message).dialog({
  buttons: {OK: function(){$(this).dialog('close'); callback()}},
  autoOpen: true,
  close:function(){$(this).remove();},
  draggable: true,
  modal: false,
  resizable: false,
  height:'auto',
  width: 'auto'
});
相关问题