要使用 JQuery UI 的 Dialog Widget,首先必須引用 JQuery、JQueryUI。
接著在畫面上放入一個 div element。
Hello World
在 Javascript 中用 JQuery 找到該 div element,並叫用 dialog 方法即可將該 div element 設為 Dialog。
$(function() {
$("#dialog").dialog();
});
{% img /images/posts/JQueryUIDialog/1.png %}
若要做些細部設定,Dialog 有提供些 options 可供我們使用,像是 resizable 可以讓 dialog 進行手動縮放、buttons 可設定 dialog 的按鈕。
$(function() {
$("#dialog").dialog(
{
resizable: true,
buttons: {
OK: function() {
$( this ).dialog( "close" );
},
Cancel: function() {
$( this ).dialog( "close" );
}
}
});
});
{% img /images/posts/JQueryUIDialog/2.png %}
若要主動觸發 Dialog,也提供了些 methods 讓我們使用,像是 open。
$("#open").click(function()
{
$("#dialog").dialog("open");
});
{% img /images/posts/JQueryUIDialog/3.png %}
最後這邊附上測試用的範例:
$(function() {
$("#dialog").dialog(
{
autoOpen: false,
resizable: true,
buttons: {
OK: function() {
$( this ).dialog( "close" );
},
Cancel: function() {
$( this ).dialog( "close" );
}
}
});
$("#open").click(function()
{
$("#dialog").dialog("open");
});
});
Hello World
Open