使用 jQuery 查找 z-index 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3236308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:04:51  来源:igfitidea点击:

Finding z-index value using jQuery

jquery

提问by Baljit

How to find a z-index value of a element by jQuery?

如何通过jQuery查找元素的z-index值?

i.e.: I have 2 divs, both are positioned absolute and z-index 10, 1000 respectively.

即:我有 2 个 div,都分别定位为绝对和 z-index 10、1000。

But unfortunately IE6 displaying second div which has z-index 1000 below the first one.

但不幸的是,IE6 显示第二个 div,其 z-index 低于第一个 1000。

So I want to check what the z-index value of second one and first one at run time in IE6.

所以我想在 IE6 中检查运行时第二个和第一个的 z-index 值。

Please help.

请帮忙。

回答by Krunal

You can get it using css.

您可以使用css.

$("#your-div").css("z-index");

Ref: http://api.jquery.com/css/

参考:http: //api.jquery.com/css/

回答by user959690

You can use $('selector').zIndex().

您可以使用$('selector').zIndex().

DO NOT USE $('selector').css('z-index'). It will only tell you what (if anything) was set in the style sheet. It may just say "auto".

不要使用$('selector').css('z-index')。它只会告诉您在样式表中设置了什么(如果有的话)。它可能只是说“自动”。

回答by Nick Craver

If they have IDs for example (or any selector you can find them by) use .css(), like this:

例如,如果他们有 ID(或您可以通过的任何选择器),请使用.css(),如下所示:

var zIndex = $("#div1").css('z-index');

Or the more complete:

或者更完整的:

alert("Div 1 zIndex: " + $("#div1").css('z-index'));
alert("Div 2 zIndex: " + $("#div2").css('z-index'));

回答by instanceof me

Like the css()function ?

css()函数?

alert($('#my_object_id').css('zIndex'));

回答by Kenil patel

jQuery provides methods to directly set and get z-indexof any element

jQuery 提供了直接设置和获取任何元素的z-index的方法

Syntax:

句法:

var index = $("#div1").zIndex()

回答by Anurag

$(elem).css('z-index');

Also, where are you setting the z-indexes. If it's in an inline style vs a style block, then you may see different results. So:

另外,你在哪里设置z-indexes。如果它是内联样式与样式块,那么您可能会看到不同的结果。所以:

<div style="z-index: 1000">..</div>

vs

对比

<style>
div {
    z-index: 1000;
}
</style>

may give different results. Also, remember that going by your logic is an exercise in vain with IE.

可能会给出不同的结果。另外,请记住,按照您的逻辑进行操作对 IE 来说是徒劳的。

回答by majwro

Besides using

除了使用

$('selector').css('z-index')

you have to add postion:absoluteor position:relativein your styles to element with z-index you want to get.

您必须将样式postion:absoluteposition:relative样式添加到您想要获取的 z-index 元素。

回答by sushil bharwani

use zIndex instead of z-index.

使用 zIndex 而不是 z-index。

$('selector').css('zIndex');

$('选择器').css('zIndex');

回答by gblazex

Your code must be broken. Check this, it works.

您的代码必须被破坏。检查这个,它有效

If you don't set a z-indexvalue FFand Chromewill display auto, and IE0.

如果您不设置z-indexFFChrome将显示auto, 和IE0

Undefinedmeans there is some error in your code.

Undefined意味着您的代码中存在一些错误

回答by Michael Lumbroso

what about something like

怎么样

var zindex = $("#id").css("z-index");