jQuery traversal find() method

jQuery is a collection object. If you want to quickly find the descendant elements of these elements in the DOM tree, you can use the find() method. This is also a method frequently used in development. Here we should pay attention to the difference between children and find methods. Children is a parent-child relationship search, and find is a descendant relationship (including parent-child relationship)

Understand the node search relationship:



  • 1



  • ##If the code is $("div").find("li"), at this time, li and div are ancestors, and can be quickly found through the find method.

    Knowledge points to note with the find() method:

    Find is to traverse the descendants of each element in the current element collection. As long as they meet the requirements, it doesn't matter whether they are sons or grandsons.

    Unlike other tree traversal methods, selector expressions are required parameters for .find(). If we need to retrieve all descendant elements, we can pass the wildcard selector '*'.

    find only traverses among descendants, excluding itself.

    The selector context is implemented by the .find() method; therefore, $('.item-ii').find('li') is equivalent to $('li', '.item- ii') (find the li tag under the tag with the class name item-ii).

    Key points to note:

    The find() and .children() methods are similar

    1.Children only searches for the first-level child nodes

    2.The search scope of find includes All descendant nodes of child nodes


    Let’s write an example

          
    

    php.cn

    • php 中文网

Continuing Learning
||

php.cn

  • php 中文网
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!