2010-08-02 97 views
13

如何在Javascript中创建自定义提醒功能?使用Javascript的自定义提醒

+0

你能说得更具描述性吗? – 2010-08-02 21:22:31

+1

我想将'http // ...'页面的默认标题更改为我自己的自定义标题。那可能吗? – 2010-08-02 21:29:13

回答

28

您可以覆盖现有的alert功能,它存在的window对象:

window.alert = function (message) { 
    // Do something with message 
}; 
10

从技术上讲,您可以更改警报功能的功能。但是,您不能更改由本机警报功能(除了文本/内容)启动的模式窗口的标题或其他行为。

+0

好的,这就是我需要知道的。 +1。但接受了advait的答案,因为他给了我一个替代解决方案。 – 2010-08-02 21:39:09

13

这是我想出的解决方案。我写了一个通用函数来创建一个jQueryUI对话框。如果你想,你可以用Matt的建议覆盖默认的警报函数:window.alert = alert2;

// Generic self-contained jQueryUI alternative to 
// the browser's default JavaScript alert method. 
// The only prerequisite is to include jQuery & jQueryUI 
// This method automatically creates/destroys the container div 
// params: 
//  message = message to display 
//  title = the title to display on the alert 
//  buttonText = the text to display on the button which closes the alert 
function alert2(message, title, buttonText) { 

    buttonText = (buttonText == undefined) ? "Ok" : buttonText; 
    title = (title == undefined) ? "The page says:" : title; 

    var div = $('<div>'); 
    div.html(message); 
    div.attr('title', title); 
    div.dialog({ 
     autoOpen: true, 
     modal: true, 
     draggable: false, 
     resizable: false, 
     buttons: [{ 
      text: buttonText, 
      click: function() { 
       $(this).dialog("close"); 
       div.remove(); 
      } 
     }] 
    }); 
} 
0

“覆盖”的方式不够好,建议您创建一个自定义弹出框。该解决方案的最大好处是可以控制每个细节。

+0

[SweetAlert](http://t4t5.github.io/sweetalert/)是您的不错选择 – dabeng 2016-02-06 03:03:25

+0

[Simple Popup](https://github.com/dabeng/Simple-Popup)是您的另一个轻量级选择。 – dabeng 2016-02-06 03:05:53

+0

我建议您编辑您的答案以包含这些链接。 – mjk 2016-02-06 03:32:27