使用jQuery添加新的父div树 [英] Using jquery to add new parent div tree

查看:73
本文介绍了使用jQuery添加新的父div树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是jquery的新手,并为此找到解决方案,

I'm new to jquery and find solution about this,

<div id="parent">
   <div class="child1">Child 1 Contents</div>
   <div class="child2">Child 2 Contents</div>
</div>

我想在<div id="parent">之后创建新的父div元素,并使用jquery将所有子项从该父项包含到新父项中,因此结果html如下所示:

I want to make new parent div element after <div id="parent"> and include all child from this parent to new parent using jquery, so the result html looks like below:

<div id="parent">
  <div id="newparent">
    <div class="child1">Child 1 Contents</div>
    <div class="child2">Child 2 Contents</div>
  </div>
</div>

如何使用jquery解决此问题?

How to solve this using jquery?

推荐答案

jQuery的 wrapInner 应该可以解决问题:

jQuery's wrapInner should do the trick:

$('#parent').wrapInner('<div class="newparent"></div>');

这篇关于使用jQuery添加新的父div树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆