我需要能够在包装集中获得最顶层的".ui-dialog"元素.
我的第一个想法是做这样的事情:
var topDialog = $(".ui-dialog").orderBy("[style*=z-index]").eq(0);
Run Code Online (Sandbox Code Playgroud)
有没有比编写循环检查值更好的方法呢?
编辑:只是为了澄清......我需要能够在页面生命周期中的任何给定点获得最顶层的元素(打开和关闭多个对话框后).Nick Craver使用maxZ变量的答案似乎不起作用,因为删除对话框时变量不会减少.
这是我现在使用的循环,当我关闭一个看起来有点难看的对话框时:
// Enable printing of the top-most dialog or #page
if ($(".ui-dialog").length > 0) {
var top = $(".ui-dialog").first();
$(".ui-dialog").each(function () {
if ($(this).css("z-index") > top.css("z-index")) {
top = $(this);
}
});
top.removeClass("dont-print");
} else {
$("#page").removeClass("dont-print");
}
Run Code Online (Sandbox Code Playgroud)
由于对话框是堆叠的,顶部的变量z-index实际上是维护和可访问的,它是:$.ui.dialog.maxZ,你可以看到它是如何在这里设置的.
因此,您可以只查看哪个对话框具有最高索引(只有一个),而不是排序,如下所示:
var topDialog = $(".ui-dialog").filter(function() {
return $(this).css("z-index") == $.ui.dialog.maxZ;
});
Run Code Online (Sandbox Code Playgroud)