這是我想出的解決方案。我編寫了一個通用函數(shù)來創(chuàng)建一個jQueryUI對話框。如果你愿意,你可以使用Matt的建議來覆蓋默認(rèn)的alert函數(shù):window.alert = alert2;
// 通用的自包含的jQueryUI替代瀏覽器默認(rèn)的JavaScript alert方法。 // 唯一的先決條件是包含jQuery和jQueryUI // 該方法自動創(chuàng)建/銷毀容器div // 參數(shù): // message = 要顯示的消息 // title = 警告框上要顯示的標(biāo)題 // buttonText = 關(guān)閉警告框的按鈕上要顯示的文本 function alert2(message, title, buttonText) { buttonText = (buttonText == undefined) ? "確定" : buttonText; title = (title == undefined) ? "頁面提示:" : 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(); } }] }); }
您可以覆蓋現(xiàn)有的alert
函數(shù),該函數(shù)存在于window
對象上:
window.alert = function (message) { // 對消息進(jìn)行處理 };