如何使用jQuery来获取具有相同z-index的元素? [英] How to use jQuery to get elements with the same z-index?
问题描述
现在,我有不同 z-index
的元素,我想使用jQuery对它们进行分组。
< div class =floatstyle =z-index:2>< / div>
< div class =floatstyle =z-index:6>< / div>
< div class =floatstyle =z-index:10>< / div>
。
。
。
<! - 你得到点 - >
我可以使用 .attr(style)
得到 z-index
值,但似乎不是一个好主意。 (有时它可能在< style>
标签中)有什么类似的吗?
var ele = $(* [zindex = 5]);期待任何解决方案。
< >解决方案创建自己的自定义选择器:
$。extend($。expr [': '],{
zindex:function(el,i,m){
return $(el).css(z-index)=== m [3];
}
});
您可以这样使用:
$(div:zindex(2))每个(function(){
alert(this.innerHTML);
}
这将返回所有 div
2
< div style =position:relative; z-index:2> Foo< / div>
< div style =position:relative; z-index:6> Bar< / div>
使用上面的标记,早期的jQuery脚本会提醒 Foo
。
演示: http:/ /jsbin.com/icevih/2/edit
Right now, I have elements with different z-index
, and I want to group them using jQuery.
<div class="float" style="z-index:2"></div>
<div class="float" style="z-index:6"></div>
<div class="float" style="z-index:10"></div>
.
.
.
<!-- You get the point -->
I can use .attr("style")
to get the z-index
value out, but it does not seem to be a good idea to do that. (and sometimes it might in the <style>
tag too) Is there anything like that?
var ele = $("*[zindex=5]");
Looking forward for any solutions.
解决方案 Create your own custom selector:
$.extend($.expr[':'], {
zindex: function(el, i, m) {
return $(el).css("z-index") === m[3];
}
});
Which you could then use like this:
$("div:zindex(2)").each(function(){
alert( this.innerHTML );
});
This will return all div
elements with a z-index
of 2
.
<div style="position:relative; z-index:2">Foo</div>
<div style="position:relative; z-index:6">Bar</div>
With the above markup, the earlier jQuery script will alert Foo
.
Demo: http://jsbin.com/icevih/2/edit
这篇关于如何使用jQuery来获取具有相同z-index的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!