jquery siblings Usage Example Analysis of Getting Peer Elements

  • 2021-07-04 18:11:03
  • OfStack

This article illustrates the use of jquery siblings to obtain peer elements. Share it for your reference, as follows:

jquery siblings is used to obtain siblings for each element in the matching set.

Syntax:

.siblings(selector)

Note: You can filter further through the optional parameter selector (selector) later.

Example:

Find all peer elements of each div.


<p>Hello</p>
<div>
  <span>www.ofstack.com</span>
</div>
<p>How are you!</p>

$("div").siblings().html();

The result will output:


<p>Hello</p>
<p>How are you!</p>

Example:

Find all sibling elements of each div with the class name selected.


<div>
<span>Hello</span>
</div>
<p class="selected">www.ofstack.com</p>
<p>How are you!</p>

$("div").siblings(".selected").html();

The result will output:


<p class="selected">www.ofstack.com</p>

For more readers interested in jQuery related content, please check the topics of this site: "Summary of Ajax Usage in jquery", "Summary of jQuery form Operation Skills", "Summary of jQuery Common Plug-ins and Usage", "Summary of jQuery Operation json Data Skills", "Summary of jQuery Extension Skills", "Summary of jQuery Drag Effects and Skills", "Summary of jQuery Table (table) Operation Skills", "Summary of jQuery Common Classic Special Effects", "Summary of jQuery Animation and Special Effects Usage" and "Summary of jquery

I hope this article is helpful to everyone's jQuery programming.


Related articles: