开发者

Finding a particular parent of an HTML DOM element

开发者 https://www.devze.com 2023-01-03 01:36 出处:网络
Lets say I have the following: <table><tr><td> <table> <!-- I want a reference to this table -->

Lets say I have the following:

<table><tr><td>
    <table> <!-- I want a reference to this table -->
        <tr>
            <t开发者_运维技巧d>
                <table>
                    <tr><td><span class="theSpanClass"></span></td></tr>
                </table>
            </td>
        </tr>
     </table>
</td></tr></table>

Using jQuery, I'd like to be able to reference the 2nd table starting from the span towards the body.

Thanks


For original question: If you're coming from the span, then you can do this:

$(".theSpanClass").parents("table:last")

This uses .parents() to get all ancestors that are <table>, then selects the :last one it finds, since they're ordered going up the DOM.


Updated for new question: Since the question's been updated, that's 2 levels up, which you would find using :eq() like this:

$(".theSpanClass").parents("table:eq(1)") //it's 0-based
0

精彩评论

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