bon*_*eat 1 html javascript jquery
我有这个HTML块:
<h2>heading A</h2>
<p>paragraph 1 of A</p>
<p>paragraph 2 of A</p>
<h2>heading B</h2>
<ul id="list-B">
<li>list B1</li>
<li>list B2</li>
</ul>
<p>paragraph 1 B</p>
<h2>...</h2>
..
Run Code Online (Sandbox Code Playgroud)
我需要抓住每个" h2及其内容"并将它们分组为以下内容:
<div class="news">
<h2>heading A</h2>
<div class="content">
<p>paragraph 1 of A</p>
<p>paragraph 2 of A</p>
</div>
</div>
<div class="news">
<h2>heading B</h2>
<div class="content">
<ul id="list-B">
<li>list B1</li>
<li>list B2</li>
</ul>
<p>paragraph 1 B</p>
</div>
</div>
...
Run Code Online (Sandbox Code Playgroud)
有什么建议?
干得好:
$( 'h2' ).each(function () {
$( this ).nextUntil( 'h2' ).andSelf().wrapAll( '<div class="news" />' );
$( this ).nextAll().wrapAll( '<div class="content" />' );
});
Run Code Online (Sandbox Code Playgroud)
现场演示: http ://jsfiddle.net/phJPq/2/
| 归档时间: |
|
| 查看次数: |
128 次 |
| 最近记录: |