获取指定元素的兄弟元素时,可以使用adjacent sibling combinator (+),其中+的两侧内容都是selector expression.
如果要获取下例中所有的 h1的直接兄弟元素h2
复制代码 代码如下:
<div>
<h1>Main title</h1>
<h2>Section title</h2>
<p>Some content...</p>
<h2>Section title</h2>
<p>More content...</p>
</div>
复制代码 代码如下:
$('h1 + h2')
// Select ALL h2 elements that are adjacent siblings of H1 elements.
复制代码 代码如下:
$('h1').siblings('h2,h3,p');
// Select all H2, H3, and P elements that are siblings of H1 elements.
复制代码 代码如下:
<ul>
<li>First item</li>
<li>Second Item</li>
<li>Third item</li>
<li>Fourth item</li>
<li>Fifth item</li>
</ul>
复制代码 代码如下:
$('li.selected').nextAll('li');
复制代码 代码如下:
$('li.selected ~ li');
复制代码 代码如下:
var topHeaders = $('h1');
topHeaders.next('h2').css('margin', '0);
新闻热点
疑难解答
图片精选