使用Javascript自定义提醒

时间:2010-08-02 21:21:26

标签: javascript

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

6 个答案:

答案 0 :(得分:29)

您可以覆盖alert对象上存在的现有window函数:

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

答案 1 :(得分:14)

这是我提出的解决方案。我写了一个通用函数来创建一个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();
            }
        }]
    });
}

答案 2 :(得分:10)

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

答案 3 :(得分:9)

如果您正在寻找javascript / html / css替代品,我建议您查看jQueryUI及其modal dialogs的实施情况。

答案 4 :(得分:0)

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

答案 5 :(得分:0)

不,您不能使用默认警报。 甚至没有格式化。 但是,我建议您使用 Sweet alert 来做到这一点。