JQuery traversal-nextUntil () and prevUntil ()

Source: Internet
Author: User
This article introduces the use of jQuery's traversal-nextUntil () method and prevUntil () method. If you need a friend, refer to nextUntil () to obtain all the sibling elements that follow each element. When there is a parameter, the search will stop until the element that matches the parameter of this method. The returned new jQuery object contains all the following sibling elements, but does not include the elements that are matched by the selector, DOM node, or passed jQuery object. If no parameter exists, all the following compatriot elements are selected, which is the same as the. nextAll () method.

Syntax 1:

The Code is as follows:

. NextUntil (selector, filter)

Syntax 2:

The Code is as follows:

. NextUntil (element, filter)

Detailed description
If a jQuery object indicating a DOM element set is specified ,. the nextUntil () method allows us to search for sibling elements following the elements in the DOM tree. When an element that is matched by the parameters of this method is encountered, the search is stopped. The returned new jQuery object contains all the following compatriot elements, but does not contain the elements that are matched by parameters.

If the selector does not match or does not specify a selector, all the following siblings are selected, and the elements selected by this method are the same as those of the. nextAll () method.

For jQuery 1.6, DOM nodes or jQuery objects, instead of selectors, can be passed to the. nextUntil () method.

This method uses an optional selector expression as its second parameter. If this parameter is specified, the elements are filtered by checking whether they match the selector.

Let's take a look at the following example:

The Code is as follows:

Term 1

Definition 1-a

Definition 1-b

Definition 1-c

Definition 1-d

Term 2

Definition 2-a

Definition 2-b

Definition 2-c

Term 3

Definition 3-a

Definition 3-b

The Code is as follows:

$ ("# Term-2"). nextUntil ("dt" ).css ("background-color", "red ");

The result is as follows:


1. Do not include yourself. That is, the above example does not include # term-2 itself.

2. do not include the elements matched by parameter 1. Remove the header and tail.

3. If the selector does not match or does not specify a selector, all followers will be selected. For example:

$("#term-2").nextUntil("dts").css("background-color", "red");

I changed the selector from the original dt to dts. The result is as follows:

The Code is as follows:

$ ("# Term-1"). nextUntil ("# term-3", "dd" ).css ("color", "blue ");
// Or use DOM elements:
// Var term3 = document. getElementById ("term-3 ");
// $ ("# Term-1"). nextUntil (term3, "dd" ).css ("color", "blue ");

$ ("# Term-1"). nextUntil ("# term-3", ". abc" ).css ("color", "blue ");

The result is as follows:

The following is an example of a selector that does not provide filtering.

$("#term-1").nextUntil("#term-3").css("color", "blue");

The result is shown in:

The prevUntil () method is similar to the nextUntil () method. The difference is that the method goes up and down.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.