The differences between children and find in Jquery are explained in detail

  • 2020-03-30 00:01:22
  • OfStack

First look at a piece of HTML code, as follows:


<table id="tb">
            <tr>
                <td>0</td>
                <td>1</td>
                <td>2</td>
            </tr>
            <tr>
                <td>3</td>
                <td>4</td>
                <td>5</td>
            </tr>
 </table>

If I want to get the value of the second td in the second tr:

Children:


$("#tb>tbody").children("tr:eq(1) td:eq(1)").html()

Find      :

$("#tb>tbody").find("tr:eq(1) td:eq(1)").html()

  As a result, the value obtained by children is: null, while the value obtained by find is: 4. Why?

  Looking up the data, children is the child element of the element, and find is all the child elements of the element.

  So back up here, we have $("#tb> Tbody ").children() gets two tr elements (not including their child td),

  The selector in children filters by condition in the two tr elements obtained, so the above method does not get the value.

  If we must use children, we can write:


$("#tb>tbody").children("tr:last").children("td:eq(1)").html()


Related articles: