jQuery Traversal-Ancestors

jQuery Traversal

What is traversal?

jQuery traversal, meaning "move", is used to "find" (or select) HTML elements based on their relationship to other elements. Start with a selection and move along this selection until you reach your desired element.

The picture below shows a family tree. With jQuery traversal, you can easily move up (ancestors), down (descendants), and horizontally (siblings) in the family tree, starting from the selected (current) element. This movement is called traversing the DOM.


## The
element is the parent element of
    and the ancestor of all content within it.

    The
      element is the parent element of the
    • element and is a child element of

      The

    • element on the left is the The parent element of ;, the child element of
        , and the descendant of
        .

        The element is a child of

      • and a descendant of both
          and
          .

          Two

        • elements are siblings (have the same parent element).

          The

        • element on the right is the parent element of , the child element of
            , and is the descendant of
            .

            The element is a child of the

          • on the right and is a descendant of both
              and
              .

              Warm reminder: Ancestor is father, grandfather, great-grandfather, etc. Descendants are children, grandchildren, great-grandchildren, etc. Siblings share the same parent.

              jQuery Traversal - Ancestor

              Ancestor is father, grandfather or great-grandfather, etc.

              With jQuery, you can traverse up the DOM tree to find the ancestors of an element.

              Traverse up the DOM tree

              These jQuery methods are useful for traversing up the DOM tree:

              parent()parents()parentsUntil()

              jQuery parent() method

              parent() method returns the direct parent element of the selected element.

              This method will only traverse the DOM tree one level up.

              jQuery parents() method

              parents() method returns the value of the selected element All ancestor elements, all the way up to the root element of the document ().

              The following example returns all ancestors of all elements

                     
              
              body (曾曾祖父元素)
              div (曾祖父元素)
                ul (祖父元素)
              • li (父元素) span

              jQuery parentsUntil() method

              parentsUntil () method returns all ancestor elements between two given elements.


          • Continuing Learning
        • ||
          body (曾曾祖父元素)
          div (曾祖父元素)
            ul (祖父元素)
          • li (父元素) span
          submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!