Custom alert using Javascript

后端 未结 5 1813
长发绾君心
长发绾君心 2020-11-29 07:43

How can I create a custom alert function in Javascript?

5条回答
  •  孤独总比滥情好
    2020-11-29 08:17

    This is the solution I came up with. I wrote a generic function to create a jQueryUI dialog. If you wanted, you could override the default alert function using Matt's suggestion: 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.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(); } }] }); }

提交回复
热议问题