NextUntil () is used to obtain all the following compatriot elements of each element. If a parameter exists, the search will stop until the element matching the parameter of this method is met. 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:
Copy codeThe Code is as follows:. nextUntil (selector, filter)
Syntax 2:
Copy codeThe 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:
Copy codeThe Code is as follows: <dl>
<Dt id = "term-1"> term 1 </dt>
<Dd> definition 1-a </dd>
<Dd class = "abc"> definition 1-b </dd>
<Dd> definition 1-c </dd>
<Dd> definition 1-d </dd>
<Dt id = "term-2"> term 2 </dt>
<Dd> definition 2-a </dd>
<Dd> definition 2-b </dd>
<Dd> definition 2-c </dd>
<Dt id = "term-3"> term 3 </dt>
<Dd> definition 3-a </dd>
<Dd> definition 3-b </dd>
</Dl>
Copy codeThe Code is as follows: $ ("# term-2"). nextUntil ("dt" ).css ("background-color", "red ");
The result is as follows:
Note:
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:
Copy codeThe 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.