简体   繁体   English

jQuery:parent()方法的等效选择器

[英]jQuery: selector equivalent for parents() method

What I want to do is select an element by id, find one of its parents, and finally select a different child of that parent. 我想要做的是通过id选择一个元素,找到它的一个父元素,最后选择该父元素的另一个子元素。 I can already do that like this: 我已经可以这样做了:

$('#id').parents('.class1').find('.class2');

However, I need to be able to do this using a single selector. 但是,我需要能够使用单个选择器执行此操作。 For example: 例如:

$('#id parents .class1 .class2');

Is there an equivalent to the parents() method using just selectors? 是否只使用parent()方法只使用选择器?

Your question is really about CSS at this point, and parent selectors aren't available in CSS. 您的问题实际上是关于CSS的,而CSS中没有父选择器。

Based on your comment above, why not set your element to a variable? 根据您上面的评论,为什么不将您的元素设置为变量?

var myEl = $('#id').parents('.class1').find('.class2');

widget(myEl);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM