使用Javascript自定义提醒

Ell*_*lle 15 javascript

如何在Javascript中创建自定义警报功能?

Mat*_*att 30

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

window.alert = function (message) {
  // Do something with message
};
Run Code Online (Sandbox Code Playgroud)


Wal*_*osz 15

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


Cri*_*hez 10

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


adv*_*ait 9

如果你正在寻找一个javascript/html/css替换,我建议你查看jQueryUI及其模态对话框的实现.