When getting the sibling elements of a specified element, you can use adjacent sibling combinator ( ), in which both sides of the content are selector expressions.
If you want to get all the direct sibling elements h2 of h1 in the following example
Main title
Section title
Some content...
Section title
More content ...
You can use it directly
$('h1 h2')
// Select ALL h2 elements that are adjacent siblings of H1 elements.
If To filter the sibling elements of h1, of course you can also use
$ ('h1').siblings('h2,h3,p');
// Select all H2, H3, and P elements that are siblings of H1 elements.
If you want to get For all sibling elements after the current element, you can use nextAll()
For example, for the following html code
- First item
- Second Item
- Third item
- Fourth item
- Fifth item
If you want to get all li elements after the second entry, you can use the following code
$('li.selected').nextAll('li');
The above example can also be implemented using general sibling combinator (~)
$('li.selected ~ li');
To obtain direct sibling elements, you can also use next() directly without using selector.
var topHeaders = $('h1');
topHeaders.next('h2').css('margin', '0);
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn