开发者

Wrapping Multiple Elements (jQuery)

开发者 https://www.devze.com 2022-12-13 12:40 出处:网络
I\'ve this piece of HTML: div.content div.one content div.two content div.three content I want to add two divs on top and bottom and wrap one div around it so it becomes:

I've this piece of HTML:

div.content
  div.one
    content
  div.two
    content
  div.three
    content

I want to add two divs on top and bottom and wrap one div around it so it becomes:

div.top
div.wrapper
  div.content
    div.one
      开发者_C百科content
    div.two
      content
    div.three
      content
div.bottom

I'm aware of the several wrap selectors (innerWrap, wrapAll etc..) but I'm not sure how to wrap 2 divs.

The following jQuery might work, but is there a better way to write it?

$('content').wrap('<div class="wrapper"></div');
$('.wrapper').before('<div class="top"></div>');
$('.wrapper').after('<div class="bottom"></div>');


I would do it in one line to minimize lookup:

$('content')
     .before('<div class="top"></div>')
     .after('<div class="bottom"></div>')
     .wrap('<div class="wrapper"></div>');
0

精彩评论

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