我有一些类似于以下的标记:
<h3>A Heading</h3>
<p>Here is a paragraph.</p>
<ul>
<li>First</li>
<li>Second</li>
</ul>
<blockquote>Here's some other block-level element</blockquote>
<h3>The Wrapping Should Stop Before This Heading, but a Second One Should Begin</h3>
<ol>
<li>First</li>
<li>Second</li>
</ol>
<p>Notice the varying block-level elements? It probably doesn't make any difference.</p>
我想将每个 h3
和直到但不包括下一个 h3
的所有内容包装在一个 div
中。
结果应该是:
<div>
<h3>A Heading</h3>
<p>Here is a paragraph.</p>
<ul>
<li>First</li>
<li>Second</li>
</ul>
<blockquote>Here's some other block-level element</blockquote>
</div>
<div>
<h3>The Wrapping Should Stop Before This Heading, but a Second One Should Begin</h3>
<ol>
<li>First</li>
<li>Second</li>
</ol>
<p>Notice the varying block-level elements? It probably doesn't make any difference.</p>
</div>
我发现类似的问题 this one .它结合使用了 .nextUntil()
和 .andSelf()
(或 .addBack()
一旦我的编辑被批准),但这会将 h3
和它们之间的内容包装在 separate div
中。
最佳答案
好像.wrapAll()
是缺失的环节。
$('h3').nextUntil('h3').addBack().wrapAll('<div>');
关于javascript - 如何在下一次出现相同元素之前包装每个元素及其所有兄弟元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18680594/