我正在尝试使用jQuery wrapAll将一个h1标签和多个p标签封装到一个div中。
这是我的HTML:
<h1>Title</h1>
<p>This is a paragraph</p>
<p>This is another paragraph</p>
<div class="img"></div>和我的jQuery:
$('h1').wrapAll('<div class="first-col" />');
$('.img').wrapAll('<div class="second-col" />');还有一台JSFIDDLE。
目前,我只能对h1标记进行包装,或者将h1和p标记分开。我想把它们都放在一个first-col目录中。
有人知道怎么做吗?
发布于 2013-08-29 20:30:54
试一试
$('h1').nextUntil('div.img').addBack().wrapAll('<div class="first-col" />');演示:Fiddle
发布于 2013-08-29 20:31:13
你有没有尝试过在选择器中添加p:
$('.postwrap').each(function(){
$(this).find('h1, p').wrapAll('<div class="first-col" />');
$(this).find('.img').wrapAll('<div class="second-col" />');
});https://stackoverflow.com/questions/18511064
复制相似问题