我需要能够在包装集中获得最顶层的“.ui-dialog”元素. 我的第一个想法是做这样的事情: var topDialog = $(".ui-dialog").orderBy("[style*=z-index]").eq(0); 有没有比编写循环检查值更好的方法呢? 编
我的第一个想法是做这样的事情:
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;
});
You can test it out here.
