我有一些类似于以下内容的标记:
<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
s和内容之间的单独div
s。
似乎.wrapAll()
是缺失的环节。
$('h3').nextUntil('h3').addBack().wrapAll('<div>');
你需要:first
选择器,否则你将包装所有的内容,而不仅仅是第一个h3
to第二h3
$('h3:first').nextUntil('h3').addBack().wrapAll('<div>');