如何为现有div添加父div

时间:2016-07-12 06:22:43

标签: jquery

我有2个div(动态创建),我想为这些现有的div添加父div。

HTML: -

<div id="a">div1</div>
<div id="b">div2</div>

输出应该是这样的: -

<div id="1">Main Div
<div id="a">div1</div>
<div id="b">div2</div>
</div>

2 个答案:

答案 0 :(得分:3)

使用.wrapAll() - 围绕匹配元素集中的所有元素包装HTML结构。

&#13;
&#13;
$( ".inner" ).wrapAll( "<div id='1' />");
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="inner" id="a">div 1</div> 
<div class="inner" id="b">div 2</div> 
&#13;
&#13;
&#13;

答案 1 :(得分:2)

向DOM元素添加类

$( ".inner" ).wrapAll( "<div id='1' />");

然后使用jQuery

#include <stdio.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

class A
{
    public:
        void aFunction()
        {
            cout<<"This is aFunction"<<endl;
        }
    private:
        void anotherFunction()
        {
            cout<<"This is anotherFunction"<<endl;
        }
};

class B: public A
{
    public:
        void anotherFunction()
        {
            cout<<"This is also anotherFunction, but it's accessible!"<<endl;
        }
};

int main(int argc, char* argv[])
{
    A firstClass;
    B coach;
    firstClass.aFunction();
    coach.anotherFunction();
    return 0;
}