JQuery `children` vs` find`

HTML

<table id='t'> <tr> <td id='foo' class='ab c'>blah</td> <td id='bar' class='a c'>bloo</td> <td id='zip' class='ab c'>blop</td> </tr> </table> 

Using jQuery, why the next children call returns 0

 $('#t').children('tr').length 

but find returns 1?

 $('#t').find('tr').length 

https://jsfiddle.net/o71x6co6/1/

+5
source share
1 answer

Because browsers automatically add a tbody element if you do not.

 console.log($('#t').children().get(0).tagName); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id='t'> <tr> <td id='foo' class='ab c'>blah</td> <td id='bar' class='a c'>bloo</td> <td id='zip' class='ab c'>blop</td> </tr> </table> 
+4
source

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


All Articles