如何使用jQuery获取具有某个CSS属性的第一个父级? [英] How to get the first parent that has a certain CSS property using jQuery?
本文介绍了如何使用jQuery获取具有某个CSS属性的第一个父级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要获得第一个拥有位置的父级:relative
。类似于下面的示例,但我的真实内容将动态生成。
I need to get the first parent that has position:relative
. Something like in the example below but my real content will be dynamically generated.
<div id="parent1" style="position:relative">
<div id="parent2">
<div id="my-element" style="position:absolute"></div>
</div>
</div>
您是否知道使用jQuery执行此操作的简单方法?
Do you know a simple way to do this using jQuery?
推荐答案
你可以过滤
的一组父母()
,并获取与您的过滤器匹配的第一个元素:
You can filter
the set of parents()
, and grab the first element matching your filter:
$('#my-element')
.parents()
.filter(function() {
return $(this).css('position') == 'relative';
}).first();
这篇关于如何使用jQuery获取具有某个CSS属性的第一个父级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文