Home > Web Front-end > JS Tutorial > body text

jQuery's descendant selector ancestor descendant introduction and example analysis

巴扎黑
Release: 2017-06-21 10:43:18
Original
2421 people have browsed it

jQuery's ancestor descendant selector (descendant selector) is used to match all descendant elements within the ancestor element and encapsulate them as jQuery Object and return.

Note: The search scope of the selector descendant is the descendant elements of the "ancestor element", whether it is the descendant element of the "ancestor element", the "grandchild", or the more "descendant" elements.

If you only want to find descendant elements, please use the descendant selector (parent > child).

Syntax

// 这里的ancestor表示具体的祖先选择器
// 这里的descendant表示具体的后代选择器
jQuery( "ancestor descendant" )
Copy after login

Parameters

Parameters                                                                                                                                                                    .

descendant A valid descendant selector.

Return value

Returns a jQuery object that encapsulates the DOM element that matches the descendant selector found within the DOM element that matches the ancestor selector.

If no DOM element matching the ancestor selector is found, or no DOM element matching the descendant selector is found within the DOM element matching the ancestor selector, an empty jQuery object is returned.

There may be multiple ancestor DOM elements that match the ancestor selector, and multiple descendant DOM elements may be found within one ancestor DOM element. The returned jQuery object encapsulates all DOM elements that meet the conditions.

Example & Description

Take the following HTML code as an example:

<div id="n1">
    <p id="n2" class="test">
        <span id="n3" class="a">Hello</span>
    </p>
    <p id="n4" class="detail">
        <span id="n5" class="b codeplayer">World
            <span id="n6" class="c">365mini.com</span>
        </span>
    </p>
</div>
Copy after login

Now, we want to find all p tags in the tag with id n1 at one time , you can write the following jQuery code:

// 选择了id分别为n2、n4的2个元素
$("#n1 p");
如果要一次性查找到p标签内的所有span标签,则对应的jQuery代码如下(上述示例HTML中所有的span标签都包括在内):
// 选择了id分别为n3、n5、n6的3个元素
$("p span");
当然,祖先选择器和后代选择器可以是任意有效的选择器,因此它们本身也可以是一个"祖先-后代"选择器。
// 选择了id为n6的一个元素
$("#n1 p.detail span.c");
Copy after login

The above is the detailed content of jQuery's descendant selector ancestor descendant introduction and example analysis. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template