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

Detailed explanation of jQuery selector (prev ~ siblings)

巴扎黑
Release: 2017-06-22 13:20:37
Original
1675 people have browsed it

jQuery'sprev ~ The siblings selector is used to match the siblings elements of all siblings after the prev element, encapsulate them into jQuery objects and return them.

Note: The search scope of the selector siblings must be the elements after the "prev element" and be sibling elements (that is, they have the same parent element as the "prev element").

Grammar

// 这里的prev表示具体的选择器
// 这里的siblings表示具体的选择器
jQuery( "prev ~ siblings" )
Copy after login

The spaces on both sides of the ~ symbol can be omitted, but it is not recommended to omit them, otherwise the characters may be too close together to affect reading.

Parameters

Parameter Description

prev A valid selector.

siblings A valid selector.

Return value

Returns a jQuery object that encapsulates the DOM elements that match the selector siblings and are located in the sibling elements after the "prev element".

If no corresponding match is found, an empty jQuery object is returned.

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>
        <span id="n4">Hello</span>
    </p>
    <p id="n5" class="detail">
        <span id="n6" class="b codeplayer">World
            <span id="n7" class="a">http://365mini.com</span>
            <span id="n8"></span>
            <span id="n9"></span>
        </span>
    </p>
</div>
Copy after login

Now, if we want to find the peer p tag after the p tag, we can write the following jQuery Code:

// 选择了id为n5的一个元素
$("p ~ p");
Copy after login

Next, we find the sibling span tags located after the span tag with id n8, then we can write the following jQuery code:

// 选择了id为n9的一个元素
// n7虽然也是n8的同辈元素,但不在n8之后,而是之前,因此匹配不到
$("#n8 ~ span");
Copy after login

Find the sibling span tags located after the span tag, The corresponding jQuery code is as follows:

// 选择了id分别为n4、n8、n9的三个元素
// n9属于n7的~siblings,也是n8的~siblings,同一个元素只计入一次
$("span ~ span");
Copy after login

The above is the detailed content of Detailed explanation of jQuery selector (prev ~ siblings). For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!