Javascript 查找具有指定z索引的元素

Javascript 查找具有指定z索引的元素,javascript,jquery,Javascript,Jquery,例如,如何查找z-index为10的HTML元素(-s)?您必须迭代所有元素并检查其z-index: $('*').filter(function() { return $(this).css('z-index') == 10; }).each(function() { // do something with them }); 一种可能的[jQuery]解决方案: $(".elementsToSearch").each(function() { if($(thi

例如,如何查找z-index为10的HTML元素(-s)?

您必须迭代所有元素并检查其z-index:

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

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

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

只需在元素中循环搜索与css规则匹配的元素

您可以获取所有元素并按css属性进行过滤:

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

在我对Chrome43的测试中,我发现这是有帮助的,但不是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);
    })
}

也可能重复:不要。如果你使用这种方法,你可能做错了什么。请改用ID或类。另外: