简体   繁体   中英

jQuery going back 2 parents

Hey all I am trying to get to the following tag:

<tr class="grid-layout-row">

in this HTML:

<tr class="grid-layout-row">
    <td role="presentation" class="grid-layout-row-leader">
        <div class="grid-layout-row-leader-spacer"></div>
    </td>
    <td role="presentation" class="grid-layout-cell  F_Form1-P_NewPage-F_DatePosted_cell">
            ....... lots of code here
    </td>
</tr>

From starting at this tag:

<td role="presentation" class="grid-layout-row-leader">
    <div class="grid-layout-row-leader-spacer"></div>
</td>

I've tried:

var curr = $('td[class*=-F_DatePosted_cell').parent().prev();

But that seems to pick up the tag before:

<tag here that it gets>
<tr class="grid-layout-row">

What would I be missing?

Don't use parent because structures change over time and best practice is to not rely on the precise position in DOM. Use closest to travel up to first parent with matching selector, like so:

$(".grid-layout-row-leader-spacer").closest(".grid-layout-row");

您应该使用parents()。

var curr = $('td[class*=-F_CheckBox2_cell').parents('.grid-layout-row');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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