Home>Article>Web Front-end> How to query elements within a node with jquery

How to query elements within a node with jquery

青灯夜游
青灯夜游 Original
2023-02-10 11:24:09 2687browse

Query method: 1. Use the children() function to query the direct subset elements in the specified node, the syntax is "$(selector).children(filter)"; 2. Use the find() function to query Query all subset elements (including subsets of subsets) within the specified node, using the syntax "$(selector).find(filter)".

How to query elements within a node with jquery

The operating environment of this tutorial: windows7 system, jquery3.6 version, Dell G3 computer.

To query the elements within a node is to query the child elements of the specified node.

There are two methods for jquery to query child elements:

  • children() method: Get the direct subset elements under the element

  • find() method: Get all subset elements (including subsets of subsets) under the element

Let’s take a look at this Two methods.

jquery children() method

children() method returns all direct child elements of the selected element.

DOM tree: This method only traverses a single level down the DOM tree. To traverse down multiple levels (returning descendant nodes or other descendants), use the find() method.

Tip: To traverse a single level up the DOM tree, or to traverse all paths up to the root element of the document (returning parent nodes or other ancestors), use the parent() or parents() method.

Note: This method does not return text nodes. To return all child nodes containing a text node, use the contents() method.

Syntax

$(selector).children(filter)
##Parameters Description Optional. Specifies a selector expression that narrows the search for child elements.
filter

Example:

       
div (父节点)
    ul (指定元素)
  • li (子节点1) span (孙节点1)
  • li (子节点2) span (孙节点2)
  • li (子节点3) span (孙节点3)

How to query elements within a node with jquery

jquery find() method

The find() method returns the descendant elements of the selected element. (Descendants are children, grandchildren, great-grandchildren, and so on.)

  • DOM tree: This method traverses all paths down the DOM element's descendants to the last descendant (< ;html>).

Syntax:


$(selector).find(filter)

Parameters Description Required. A selector expression, element, or jQuery object that filters search descendants.
filter

Note:To return multiple descendants, use commas to separate each expression.
Description: The filter parameter is used to filter the search results. You only need to set the parameter to "*" to find all sub-elements.

Example: Query all child elements

       
div (父节点)
    ul (指定元素)
  • li (子节点1) span (孙节点1)
  • li (子节点2) span (孙节点2)
  • li (子节点3) span (孙节点3)

How to query elements within a node with jquery

[Recommended learning:

jQuery video tutorial,web front-end video

The above is the detailed content of How to query elements within a node with jquery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn