2012-04-13 95 views
10

如何查找含有z-index = 10的HTML元素(-s)?查找具有指定z索引的元素

+0

可能重复[jQuery的:你可以通过CSS规则,不是阶级选择?(http://stackoverflow.com/questions/43926/jquery-can-you- select-by-css-rule-not-class) – 2012-04-13 15:38:17

+0

另请参见:[jQuery通过内联css属性查找](http://stackoverflow.com/questions/1180067/jquery-find-by-inline-css-attribute) – aruseni 2012-04-13 15:39:24

+1

Don “T。如果你使用这种方法,你可能做错了什么。改为使用ID或类。 – Blazemonger 2012-04-13 15:39:42

回答

17

你必须遍历所有元素,并检查他们的z-index:

$('*').filter(function() { 
    return $(this).css('z-index') == 10; 
}).each(function() { 
    // do something with them 
}); 
1

一个可能的[jQuery的]解决方案:

$(".elementsToSearch").each(function() 
{ 
    if($(this).css('z-index') == 10) 
    { 
     //then it's a match 
    } 
}); 

只是遍历元素寻找匹配的CSS规则。

1

你可以得到所有的元素和CSS属性进行筛选:

$('*').each(function(){ 
    if($(this).css('z-index') == 10) { 
     //$(this) - is element what you need 
    } 
}); 
0

在我在Chrome 43测试中,我发现@ThiefMaster's post是有益的,但不是100%。被拉的z-index的值是一个字符串。

我也使这只能迭代可见元素,并处理auto

这里是我的更新:的

var topZ = $('.thing-in-front').css('z-index') 
if (topZ != 'auto') { 
    topZ = parseInt(topZ); 
    $('*:visible').filter(function() { 
     var thisZ = $(this).css('z-index') 
     return thisZ != 'auto' && parseInt(thisZ) >= topZ; 
    }).each(function() { 
     $(this).css('z-index', topZ - 1); 
    }) 
}