Finding the specific parent of an HTML DOM element

Say I have the following:

<table><tr><td> <table> <!-- I want a reference to this table --> <tr> <td> <table> <tr><td><span class="theSpanClass"></span></td></tr> </table> </td> </tr> </table> </td></tr></table> 

Using jQuery, I would like to be able to reference the second table, starting with a span over the body.

thanks

+4
source share
1 answer

For the original question: If you are out of range, you can do this:

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

Here, .parents() is used to get all the <table> ancestors, then selects :last , which it finds, since they are ordered going up the DOM.


Updated for new question:. Since the question has been updated, these are 2 levels up, which you will find with :eq() :

 $(".theSpanClass").parents("table:eq(1)") //it 0-based 
+5
source

Source: https://habr.com/ru/post/1312331/


All Articles