开发者

Traverse up until some element

开发者 https://www.devze.com 2023-02-15 01:42 出处:网络
I have some element selected with jQuery selector. It is en element from list of elements with the same class (\'my-elements\'). Now I\'d like to select element containing the selected one which is se

I have some element selected with jQuery selector. It is en element from list of elements with the same class ('my-elements'). Now I'd like to select element containing the selected one which is several levels above. How can I do this ?

<div class="vote>
(... several levels)
<div class="my_element"></div>
</div>

the selector:

var ele开发者_如何学JAVAments = $('.my_element');
var element = elements[0];


The .closest() method should work (http://api.jquery.com/closest/):

elements.closest("div.vote");


If I understand it correctly you want to traverse to div.votes from div.my_element Try this:

var elements = $('.my_element'); 
var element = elements.closest("div.vote"); 


If you're attempting to find the container element for your current object I suggest the parents() method. It takes a selector, or without one, returns all of the parents of the element.

0

精彩评论

暂无评论...
验证码 换一张
取 消