jquery parentsUntil() method
Translation results:
parents
UK['peərənts] US['peərənts]
n.Parents (plural noun of parent)
until
English[ənˈtɪl] US[ʌnˈtɪl]
prep.Until...;before...
conj.Until..., before...;until …talent
jquery parentsUntil() methodsyntax
Function: parentsUntil() Gets the ancestor elements of each element in the current matching element set, until (but not including) the element matched by the selector, DOM node or jQuery object.
Syntax 1: .parentsUntil(selector,filter)
Parameters:
Parameter | Description |
selector | Optional. A string value that specifies the selector expression where to stop matching ancestor elements. |
filter | Optional. String value containing the selector expression used to match the element. |
Syntax 2: .parentsUntil(element,filter)
Parameters:
Parameters | Description |
element | Optional. A DOM node or jQuery object indicating where to stop matching ancestor elements. |
filter | Optional. String value containing the selector expression used to match the element. |
Description: If given a jQuery object that represents a collection of DOM elements, the .parentsUntil() method allows us to search for these elements in the DOM tree. Ancestor elements until an element matched by the selector (the argument passed to the method) is encountered. The returned jQuery object contains all ancestor elements except the one matched by the selector specified by the .parentsUntil() method. If there is no match or no selector is applied, all ancestor elements will be selected; in this case, the method selects the same elements as .parents() if no selector is provided. As of jQuery 1.6, a DOM node or jQuery object, instead of a selector, can be used as the first argument to the .parentsUntil() method. This method accepts an optional selector expression as its second parameter. If this parameter is applied, elements will be filtered by testing whether they match this selector.
jquery parentsUntil() methodexample
<!DOCTYPE html> <html> <head> <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script> </head> <body> <ul class="level-1 yes"> <li class="item-i">I</li> <li class="item-ii">II <ul class="level-2 yes"> <li class="item-a">A</li> <li class="item-b">B <ul class="level-3"> <li class="item-1">1</li> <li class="item-2">2</li> <li class="item-3">3</li> </ul> </li> <li class="item-c">C</li> </ul> </li> <li class="item-iii">III</li> </ul> <script> $("li.item-a").parentsUntil(".level-1") .css("background-color", "red"); $("li.item-2").parentsUntil( $("ul.level-1"), ".yes" ) .css("border", "3px solid blue"); </script> </body> </html>
Click the "Run instance" button to view the online instance