2013-05-01 102 views
1

我试图做的魔力,把我所有的平台的JavaScript警告jQuery的对话,我也跟着下面的脚本重写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> 

,但没有运气。

这是干净的解决方案吗?谢谢,

+0

你在控制台中的任何消息?你已经正确地将jQuery和jQuery UI加载到页面中了,是吗? – Marc 2013-05-01 02:56:15

+0

你面临的问题是什么 – 2013-05-01 02:56:39

+2

对我来说看起来很好http://plnkr.co/edit/mzSKFsjvkSod1oqErdFF?p=preview – 2013-05-01 02:58:19

回答

8

我宁愿一个动态的,而不是div

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

DEMO.

+0

感谢的人,说我有一个链接,点击时,一个警告弹出,这也帮助该场景呢? – ComeRun 2013-05-01 03:59:43

3

它只是工作。

检查jsfiddle demo

注意:您不能直接在<head><script>标签内调用alert('foo');标签,因为div元素在dom上没有准备好。

+0

感谢,并解决方案涵盖上弹出点击警报?说你点击一个链接,并弹出一个确认弹出窗口。我想要得到jQuery的对话框时,点击这些类型的链接 – ComeRun 2013-05-01 04:01:09

0

您的代码看起来不错,但要确保你添加jQuery和jQuery的UI库到您的网页。

演示:Plunker

-1

如果我们提交页面会自动警报之前。 一些保存成功的消息,但没有要求“好”。 我已经完成了alert.any建议的重写。 window.alert =功能(消息,回退){

$(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' 
});