简体   繁体   English

从子级到最接近的已过滤父级的jQuery树遍历

[英]jQuery tree traversal from child to closest filtered parent

I have this code snippet from an html page: 我有一个html页面的代码片段:

<td class="alltablergt ">
    <div class="allocsz">
        <table class="table-allocsz">
        <tbody>
        <tr>
    <td>
<div class="sigle-sz">
    <span class="label-sz">36</span>
    <input class="size-quantity" type="tel" value="" name="" >
    <div class="available yes"><i aria-hidden="true" class="availablespot"></i></div>
</div>

I am in the input element "size-quantity" and I would reach the "alltablergt" element. 我在输入元素“ size-quantity”中,并且到达了“ alltablergt”元素。

If $this is the size-quantity element, I thought the way to reach the "alltablergt" element was: 如果$ this是size-quantity元素,我认为到达“ alltablergt”元素的方式是:

$(this).parentsUntil(".alltablergt")

but this doesen't work. 但这不起作用。 How can I reach it? 我如何到达?

You could use closest() . 您可以使用closest()

 $('.size-quantity').closest(".alltablergt").addClass('highlight'); 
 .highlight { background: orange; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <div class="alltablergt "> <div class="allocsz"> <table class="table-allocsz"> <tbody> <tr> <td> <div class="sigle-sz"> <span class="label-sz">36</span> <input class="size-quantity" type="tel" value="" name=""> <div class="available yes"><i aria-hidden="true" class="availablespot"></i> </div> </div> </td> </tr> </tbody> </table> </div> </div> 

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

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