包装多个元素的最佳方式是什么。我尝试了几种方法但没有成功。
这是标记:
<div>
<h3>Civil aerospace </h3>
<p>Powering more than 30 civil aircraft types from small executive jets to the lartest airliners.</p>
<h4>£47.1bn</h4>
<p>Order book</p>
<h4>£4,481m</h4>
<p>Revenue</p>
</div>
这就是我需要结束的:
<div class="hub">
<h3>Civil aerospace </h3>
<p>Powering more than 30 civil aircraft types from small executive jets to the lartest airliners.</p>
<div class="wrap">
<h4>£47.1bn</h4>
<p>Order book</p>
</div>
<div class="wrap">
<h4>£4,481m</h4>
<p>Revenue</p>
</div>
</div>
已尝试添加、查找、过滤等
我需要设置迭代吗?
最佳答案
抱歉,不得不出去吃点油炸食品 :)
Scharrels 是正确的,但这里还有一个适用于 jQuery 1.3.2 的可能解决方案:
$(function(){
$('div').addClass('hub').find('h4').wrap('<div class="wrap"></div>');
$('.wrap').each(function(){
var div = $(this);
div.append(div.next('p').remove());
while(div.next('p').length != 0)
div.append(div.next('p').remove());
});
});
这有点繁琐,但它确实有效,而且它确实满足了多个 <p>
的需求。每个 <h4>
之后的标签.
关于javascript - .wrap() 和多元素选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2161306/