{"確定":function(){},"取消":function(){}}[{text:"確定", click:function(){}},{text:"取消",click:function(){}}]較少用到的還有:closeOnEscape(預設 true,按 Esc 關閉)、show / hide(開關動畫)、position(預設 "center",可寫成 'left'、['right','top'] 或絕對座標 [350,100])、minWidth 與 minHeight(預設皆為 150)。
$("...").dialog({
title: "標題",
// 其他參數
});
var dlg = $("...").dialog({ /* 參數 */ });
dlg.dialog("option", { title: "標題" });
dlg.dialog("open");
open、close、create、resize、drag,寫法與參數一致。例如開啟時把右上角的關閉鈕藏起來:
$("...").dialog({
open: function (event, ui) {
$(".ui-dialog-titlebar-close", $(this).parent()).hide();
}
});
把常見的提示、確認、iframe 視窗包成靜態方法,之後一句話就能叫出來:
jQuery.extend(jQuery, {
// 一般提示
jqalert: function (text, title, fn) {
var html =
'<div class="dialog" id="dialog-message">' +
' <p><span class="ui-icon ui-icon-circle-check" style="float:left;margin:0 7px 0 0;"></span>' + text +
' </p></div>';
return $(html).dialog({
resizable: false,
modal: true,
show: { effect: 'fade', duration: 300 },
title: title || "提示資訊",
buttons: {
"確定": function () {
var dlg = $(this).dialog("close");
fn && fn.call(dlg);
}
}
});
},
// 倒數後自動關閉
jqtimeralert: function (text, title, fn, timerMax) {
var dd = $('<div class="dialog" id="dialog-message"><p>' + text + '</p></div>');
dd[0].timerMax = timerMax || 3;
return dd.dialog({
resizable: false,
modal: true,
show: { effect: 'fade', duration: 300 },
open: function () {
var me = this, dlg = $(this),
btn = dlg.parent().find(".ui-button-text").text("確定(" + me.timerMax + ")");
--me.timerMax;
me.timer = window.setInterval(function () {
btn.text("確定(" + me.timerMax + ")");
if (me.timerMax-- <= 0) {
dlg.dialog("close");
fn && fn.call(dlg);
window.clearInterval(me.timer);
}
}, 1000);
},
title: title || "提示資訊",
buttons: {
"確定": function () {
var dlg = $(this).dialog("close");
fn && fn.call(dlg);
window.clearInterval(this.timer);
}
},
close: function () { window.clearInterval(this.timer); }
});
},
// 確認框
jqconfirm: function (text, title, fn1, fn2) {
var html =
'<div class="dialog" id="dialog-confirm">' +
' <p><span class="ui-icon ui-icon-alert" style="float:left;margin:0 7px 20px 0;"></span>' + text +
' </p></div>';
return $(html).dialog({
resizable: false,
modal: true,
show: { effect: 'fade', duration: 300 },
title: title || "提示資訊",
buttons: {
"確定": function () {
var dlg = $(this).dialog("close");
fn1 && fn1.call(dlg, true);
},
"取消": function () {
var dlg = $(this).dialog("close");
fn2 && fn2(dlg, false);
}
}
});
},
// iframe 視窗
jqopen: function (url, options) {
var html =
'<div class="dialog" id="dialog-window" title="提示資訊">' +
'<iframe src="' + url + '" frameBorder="0" style="border:0;" scrolling="auto" width="100%" height="100%"></iframe>' +
'</div>';
return $(html).dialog($.extend({
modal: true, closeOnEscape: false, draggable: false, resizable: false,
close: function () { $(this).dialog("destroy"); }
}, options));
}
});
<div>
<input type="button" id="button1" value="普通提示" />
<input type="button" id="button2" value="自動關閉提示" />
<input type="button" id="button3" value="確認提示" />
<input type="button" id="button5" value="開啟視窗" />
</div>
<script>
$(function () {
$("#button1").click(function () { $.jqalert("這是普通提示!"); });
$("#button2").click(function () {
$.jqtimeralert("這是自動關閉的提示!", "自動關閉提示", function () { $.jqalert("時間到"); });
});
$("#button3").click(function () {
$.jqconfirm("確定要這麼做嗎?", "確認提示",
function () { $.jqalert("點了確定"); },
function () { $.jqalert("點了取消"); });
});
$("#button5").click(function () {
$.jqopen("https://lwme.cnblogs.com/", { title: "我的部落格", width: 700, height: 500 });
});
});
</script>
第一,上面的寫法每次關閉後並未銷毀實例,容易累積資源。三種解法:在 close 事件裡 destroy、把 alert/confirm 內的 dialog 實例改為靜態、或在外部統一複用單一實例。
第二,jQuery UI 的字級以 em 為單位,dialog 顯示出來常會偏大,補上這幾行會正常許多:
body { font-size: 12px; }
.ui-widget { font-size: 1em; }
.ui-dialog .ui-dialog-buttonpane { padding-top: .1em; padding-bottom: .1em; }
function radcallback(s) {
return Function.createDelegate(s, function (argument) {
if (argument) { this.click(); }
});
}
function radconfirm2(textOrFn, title, callback) {
return function (s, e) {
$.jqconfirm(textOrFn, title, callback || radcallback(s));
e.set_cancel(true);
};
}
// 使用
<telerik:RadButton ... OnClientClicking="radconfirm2('確定要這麼做嗎?')" />
更多參數與範例可參考官方展示頁 jqueryui.com/demos/dialog。