简体   繁体   中英

How to create a child div that would wrap around all other child divs

I have this html structure:

<div class="a">
    <div class="b"></div>
    <div class="c"></div>
    <div class="d"></div>
</div>

How can would I use jquery to add child div to div "a" that would wrap around "b", "c" and "d"? Basically, the final html I want should look like this:

<div class="a">
    <div class="child">
        <div class="b"></div>
        <div class="c"></div>
        <div class="d"></div>
    </div>
</div>

Thanks

Try,

$('.a > div').wrapAll($('<div/>',{'class' : 'child'}));

DEMO

Try this:

$(function() {
   $('.a > div').wrapAll('<div class="child"></div>');
});

use .wrapInner()

$('.a').wrapInner('<div class="test"></div>')

Working Demo

or

use .wrapAll() :

 $('.a div').wrapAll('<div class="child"></div>')

Working Demo

$('.a').html('<div class="child">'+$('.a').html()+'</div>');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM