I have a table structure like this:
<table1> <tbody> <tr> <td></td> ... <td> <table2> <tbody> <tr> <td></td> </tr> </tbody> </table> </td> </tr> </tbody> </table>
In javascript, I have a variable tbl
with value of $(table1)
, and then I want to get all direct child elements (tr) of <tbody>
of table1
. My code is :
$('tr', tb1)
Apparently it returns all <tr>
elements in table1 and table2. I think I can get by
$('tr', tb1).not(function(){return $(this).parent().parent()[0] != tb1;})
or this kind of logic.
I know $('table1 > tbody > tr')
can get the direct child tr
. Unfortunately I can not use this.
Anyone has good idea about this?
Thanks.
The ("parent > child") selector selects all elements that are a direct child of the specified element.
Definition and Usage. The children() method returns all direct children of the selected element. The DOM tree: This method only traverse a single level down the DOM tree. To traverse down multiple levels (to return grandchildren or other descendants), use the find() method.
jQuery children() method is used to get the direct children of the selected HTML element. You can use children() method to traverse through the child elements of the selected parent element.
Answer: Use the jQuery find() Method You can use the find() method to get the children of the $(this) selector using jQuery. The jQuery code in the following example will simply select the child <img> element and apply some CSS style on it on click of the parent <div> element.
You can use find()
:
tbl.find("> tbody > tr")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With