Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does jquery get the first few sibling elements

2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how jquery obtains the first few sibling elements". In daily operation, I believe that many people have doubts about how jquery obtains the first few sibling elements. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "how jquery gets the first few sibling elements". Next, please follow the editor to study!

There are three methods: 1, use prev (), get the previous sibling element, syntax "element .prev ()"; 2, use prevAll (), you can get all the previous sibling elements, syntax "elements all ()"; 3, use prevUntil (), syntax "element .elements Until (stop)".

The operating environment of this tutorial: windows7 system, jquery1.10.2 version, Dell G3 computer.

Three methods of obtaining the first few sibling elements by jquery

The prev () method, which is mainly used to get the sibling element at the next level of the specified element

The prevAll () method, which is mainly used to get all the sibling elements one level above the specified element

The prevUntil () method, which is mainly used to get the previous sibling of the specified element, which must be the element between the specified element and the element set by the prevUntil () method

This method returns all sibling elements before each element between two given parameters

1. Use the prev () method

The prev () method returns the previous sibling of the selected element.

Note: this method returns only one element.

Syntax:

The $(selector) .prev (filter) parameter description filter is optional. Specifies a selector expression that narrows the search for the previous sibling element.

Example: returns the previous sibling of an element with the class name "start"

.siblings * {display: block; border: 2px solid lightgrey; color: lightgrey; padding: 5px; margin: 15px } $(document) .ready (function () {$("li.start"). Prev (). Css ({"color": "red", "border": "2px solid red"});}) Ul (parent node) li (sibling node) li (previous sibling node of li node with class name "start") Li (li node named "start") li (sibling node) li (sibling node)

2. Use the prevAll () method

The prevAll () method returns all sibling elements before the selected element.

Syntax:

The $(selector) .describe all (filter) parameter description filter is optional. A selector expression that specifies a selector expression that narrows the range of sibling elements before the search element.

Note: if you want to return multiple sibling elements, separate each expression with a comma.

Example: returns all sibling elements before the element with the class name "start":

.siblings * {display: block; border: 2px solid lightgrey; color: lightgrey; padding: 5px; margin: 15px } $(document) .ready (function () {$("li.start"). PrevAll (). Css ({"color": "red") "border": "2px solid red"}) }) Ul (parent) li (previous sibling of li with class name "start") li (previous sibling of li with class name "start") Li (last sibling of li with class name "start") li (li node with class name "start") li (sibling) li (sibling)

3. Use the prevUntil () method

The prevUntil () method returns all sibling elements before each element between selector and stop.

The $(selector) .describe Until (stop,filter) parameter description stop is optional. Selector expressions, elements, and jQuery objects that represent sibling elements that match before the search for an element is stopped. Filter is optional. A selector expression that specifies that the search for sibling elements between selector and stop is narrowed.

Note: if you want to return multiple sibling elements, separate each expression with a comma.

Example: returns all sibling elements between two elements of the class named "start" and "stop"

.siblings * {display: block; border: 2px solid lightgrey; color: lightgrey; padding: 5px; margin: 15px } $(document) .ready (function () {$("li.start"). PrevUntil ("li.stop"). Css ({"color": "red") "border": "2px solid red"}) }) Ul (parent node) li (sibling node with class name "stop") li (previous sibling node of li node with class name "start") Li (previous sibling node of li node named "start") li (previous sibling node of li node named "start") li (li node named "start") li (sibling node) Li (sibling node)

At this point, the study on "how jquery acquires the first few sibling elements" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report