【发布时间】:2014-11-12 21:24:16
【问题描述】:
假设我有十个不同的对话框弹出窗口,每个都有自己的初始化和选项。
当任何对话框打开时,我将如何调用函数?即以下内容的正确语法:
$(if("*").dialog("open")) $(// do this);
【问题讨论】:
标签: jquery jquery-ui dialog jquery-ui-dialog
假设我有十个不同的对话框弹出窗口,每个都有自己的初始化和选项。
当任何对话框打开时,我将如何调用函数?即以下内容的正确语法:
$(if("*").dialog("open")) $(// do this);
【问题讨论】:
标签: jquery jquery-ui dialog jquery-ui-dialog
您可以监听 jQuery UI 发出的dialogopen 事件,以便在打开对话框时执行一个函数。
例如:
$(function() {
$(".dialog").dialog({
autoOpen: false
});
$("button").click(function() {
$(".dialog").dialog("close").eq($(this).index("button")).dialog("open");
});
});
$(".dialog").on("dialogopen", function() {
alert("a dialog opened!");
})
<link href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<button>Open dialog1</button>
<div class="dialog" title="Basic dialog1">
<p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>
</div>
<button>Open dialog2</button>
<div class="dialog" title="Basic dialog2">
<p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>
</div>
<button>Open dialog2</button>
<div class="dialog" title="Basic dialog3">
<p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>
</div>
或者在动态生成对话框的情况下将监听器绑定到document:
$(document).on("dialogopen", function() {
alert("a dialog opened!");
});
【讨论】: