我需要能够获取包装集中最顶层的“.ui-dialog”元素。
我的第一个想法是做这样的事情:
var topDialog = $(".ui-dialog").orderBy("[style*=z-index]").eq(0);
有没有比编写循环来检查值更好的方法?
编辑:只是为了澄清...我需要能够在页面生命周期内的任何给定点获取最顶层的元素(在打开和关闭多个对话框之后)。 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");
}
最佳答案
由于对话框是堆叠的,因此顶部 z-index
的变量实际上是可维护和可访问的,它是:$.ui.dialog.maxZ
, you can see how it's set here .
因此,您无需排序,只需查看哪个对话框具有最高索引(只有一个会),如下所示:
var topDialog = $(".ui-dialog").filter(function() {
return $(this).css("z-index") == $.ui.dialog.maxZ;
});
关于jquery - 我如何订购 jQuery 包装套装?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4299901/