如何使用jquery将整个部分添加到一组html元素中?

如何使用jquery将整个部分添加到一组html元素中?,jquery,Jquery,我想在div id=“primary”的内容中添加class=“box”的一部分,不包括h1。我的html在下面 <div id="primary"> <h1 class="page-title">Search Results for: dffd</h1> <h2>Nothing Found</h2> <p>Sorry, but nothing matched yo

我想在div id=“primary”的内容中添加class=“box”的一部分,不包括h1。我的html在下面

<div id="primary">

    <h1 class="page-title">Search Results for: dffd</h1>        

        <h2>Nothing Found</h2>
        <p>Sorry, but nothing matched your search criteria. Please try again with some different keywords.</p>


 </div> <!--end #primary -->

搜索结果:dffd
什么也没找到
抱歉,没有与您的搜索条件匹配的内容。请使用其他关键字重试

所以我想要实现的是

<div id="primary">

  <h1 class="page-title">Search Results for: dffd</h1>  

 <section class="box">



        <h2>Nothing Found</h2>
        <p>Sorry, but nothing matched your search criteria. Please try again with some different keywords.</p>

   </section>

 </div> <!--end #primary -->

搜索结果:dffd
什么也没找到
抱歉,没有与您的搜索条件匹配的内容。请使用其他关键字重试

编辑:犯了一个错误,我不希望我的h1出现在该部分,已修改了问题

使用:

$('#primary').wrapInner('');
--编辑--

看到您修改的问题,请尝试以下操作:

// Wrap the contents in with the section
$('#primary').wrapInner('<section class="box" />');

// Detach (remove, but retain events/data) the <h1>, then prepend it to the primary div
$('#primay .box h1').detach().prependTo('#primary');
//将内容与节一起包装
$('#primary').wrapInner('');
//分离(删除但保留事件/数据),然后将其前置到主div
$('#primary.box h1').detach().prependTo('#primary');
当然,有很多方法可以做到这一点

干杯

使用:

$('#primary').wrapInner('');
--编辑--

看到您修改的问题,请尝试以下操作:

// Wrap the contents in with the section
$('#primary').wrapInner('<section class="box" />');

// Detach (remove, but retain events/data) the <h1>, then prepend it to the primary div
$('#primay .box h1').detach().prependTo('#primary');
//将内容与节一起包装
$('#primary').wrapInner('');
//分离(删除但保留事件/数据),然后将其前置到主div
$('#primary.box h1').detach().prependTo('#primary');
当然,有很多方法可以做到这一点

干杯

$('#primary').children().not('h1').wrapAll('');
$('#primary').children().not('h1').wrapAll('');

谢谢。我犯了个错误!请看修改后的问题,我不希望我的H1出现在部分中。如果不可能,我可以在我想要的部分周围添加一个空div。Hanks会将你的标记为正确,但由于我已经进行了编辑,上面的内容现在更有意义。我没有投反对票,只是将其取消标记为正确,并在上面标记为正确,而不是代替感谢。我犯了个错误!请看修改后的问题,我不希望我的H1出现在部分中。如果不可能,我可以在我想要的部分周围添加一个空div。Hanks会将你的标记为正确,但由于我已经进行了编辑,上面的内容现在更有意义。我没有否决它,只是将其取消标记为正确,并在上面标记为正确,
wrapAll
。好的。我将保留完整的答案,但我将更新您的答案。卓越的功能,不知道它存在是的,
wrapAll
。好的。我将保留完整的答案,但我将更新您的答案。卓越的功能,我不知道它的存在
$('#primary').children().not('h1').wrapAll('<section class="box"></section>');