开发者

Use jQuery's .wrap on all children of a given tag

开发者 https://www.devze.com 2022-12-11 16:52 出处:网络
Working jQuery, what I\'d like to perform is something similar to the following: $(\'sometag\').children().wrap(\'<div></div>\');

Working jQuery, what I'd like to perform is something similar to the following:

$('sometag').children().wrap('<div></div>');

With the resulting DOM manipulation wrapping all children in one div, not each child individually.

Starting example HTML:

<div>
   <h3>Heading</h3>
   <p>Paragraph</p>
</div>

What this line of code does:

<div>
   开发者_开发问答 <div>
        <h3>Heading</h3>
    </div>
    <div>
        <p>Paragraph</p>
    </div>
</div>

What I want:

<div>
    <div>
        <h3>Heading</h3>
        <p>Paragraph</p>
    </div>
</div>

What is the proper syntax to achieve the end result I'm looking for?


$('sometag').children().wrapAll("<div></div>");


$('tag').wrapInner('<div>');


$('sometag').html('<div>' + $('sometag').html() + '</div>');
0

精彩评论

暂无评论...
验证码 换一张
取 消