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.
- and the ancestor of all content within it.
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
- and a descendant of both
- and
- 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
- on the right and is a descendant of both
- and
- li (父元素) span
.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 (祖父元素)
jQuery parentsUntil() method
parentsUntil () method returns all ancestor elements between two given elements.
.Continuing LearningThe element is a child of the
- on the right and is a descendant of both
.Two
- elements are siblings (have the same parent element).
.The element is a child of
- Course Recommendations
- Courseware download
-
IntermediateFront-end Vue3 actual combat [handwritten vue project]
2857 people are watching -
ElementaryAPIPOST tutorial [Popularization of technical concepts related to network communication]
1795 people are watching -
IntermediateIssue 22_Comprehensive actual combat
5521 people are watching -
ElementaryIssue 22_PHP Programming
5172 people are watching -
ElementaryIssue 22_Front-end development
8713 people are watching -
IntermediateBig data (MySQL) video tutorial full version
4525 people are watching -
ElementaryGo language tutorial-full of practical information and no nonsense
2794 people are watching -
ElementaryGO Language Core Programming Course
2814 people are watching -
IntermediateJS advanced and BootStrap learning
2563 people are watching -
IntermediateSQL optimization and troubleshooting (MySQL version)
3374 people are watching -
IntermediateRedis+MySQL database interview tutorial
2963 people are watching -
ElementaryDeliver food or learn programming?
5708 people are watching
The courseware is not available for download at the moment. The staff is currently organizing it. Please pay more attention to this course in the future~Students who have watched this course are also learning
- Let's briefly talk about starting a business in PHP
- Quick introduction to web front-end development
- Large-scale practical Tianlongbabu development of Mini version MVC framework imitating the encyclopedia website of embarrassing things
- Getting Started with PHP Practical Development: PHP Quick Creation [Small Business Forum]
- Login verification and classic message board
- Computer network knowledge collection
- Quick Start Node.JS Full Version
- The front-end course that understands you best: HTML5/CSS3/ES6/NPM/Vue/...[Original]
- Write your own PHP MVC framework (40 chapters in depth/big details/must read for newbies to advance)
- About us Disclaimer Sitemap
- php.cn:Public welfare online PHP training,Help PHP learners grow quickly!
- element on the left is the The parent element of ;, the child element of
- element is the parent element of the