如何找到文档中最高的 z-index,无论它们是什么标签?
我找到了这段代码,但我测试了它,但它不起作用,
//include jQuery.js -- visit: http://jquery.com/
$(function(){
var maxZ = Math.max.apply(null,$.map($('body > *'), function(e,n){
if($(e).css('position')=='absolute')
return parseInt($(e).css('z-index'))||1 ;
})
);
alert(maxZ);
});
有更好的方法来做到这一点吗?
编辑:
如果我将代码更改为这样,似乎可以工作,
$('body *')
$('body > *') --> is for div only I guess.
最佳答案
这不是最有效的解决方案,但它应该可行。 jsFiddle .
请注意,您必须指定一个位置,以便 z-index 返回值。
var highest = -999;
$("*").each(function() {
var current = parseInt($(this).css("z-index"), 10);
if(current && highest < current) highest = current;
});
alert(highest);
关于jquery - 如何找到文档中最高的 z-index,无论它们是什么标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17686544/