Javascript 使用wrapAll-jQuery选择不同的元素

Javascript 使用wrapAll-jQuery选择不同的元素,javascript,jquery,html,css,dom,Javascript,Jquery,Html,Css,Dom,我试图使用jquerywrapall将一个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-c

我试图使用jquerywrapall将一个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" />');
还有一个

目前,我只能环绕h1标记,或者分别环绕h1和p标记。我希望他们两个都在一个第一组

有人知道怎么做吗?

试试看

$('h1').nextUntil('div.img').addBack().wrapAll('<div class="first-col" />');
演示:

试试看

$('h1').nextUntil('div.img').addBack().wrapAll('<div class="first-col" />');

演示:

您是否尝试将p添加到选择器中:

$('.postwrap').each(function(){
    $(this).find('h1, p').wrapAll('<div class="first-col" />');
    $(this).find('.img').wrapAll('<div class="second-col" />');
});

您是否尝试在选择器中添加p:

$('.postwrap').each(function(){
    $(this).find('h1, p').wrapAll('<div class="first-col" />');
    $(this).find('.img').wrapAll('<div class="second-col" />');
});

这很有效。但是,当我在多个包装中有多个标签时,它似乎变得一团糟。请看这里:。我正试着把它们包在每一个包装纸里。你是一个美丽的标本。效果很好。但是,当我在多个包装中有多个标签时,它似乎变得一团糟。请看这里:。我正试着把它们包在每一个信封里。你是一个美丽的标本。