开发者

Select all except specific div and everything inside

I'm trying to wrap everything within the body in another div, except one div ID and everything inside it. I tried this:

$('div:not(#dontselect)').wrap('<div class="wrapper" />');

It sorta works, but everything inside #dontselect also get wrapped in .wrapper classes. I want #dontselect a开发者_开发知识库nd everything inside it to remain how they were. Any help?


You can add another :not:

$('div:not(#dontselect):not(#dontselect div)')

This excludes all div descendants of #dontselect.


do you need to wrap div childs? to select only divs in body without their childs use

$('body > div:not(#dontselect)').wrapp('<div />');


you can also do it in 3 steps

$('body')
   .find('div')             // step 1, find all divs
   .filter('#dontselect')     // step 2, add class 'no-wrapp'
      .addClass('no-wrapp')
         .find('div').addClass('no-wrapp')
   .end().end()
   .filter('div:not(.no-wrapp)').wrap('<div class="wrapper" />'); // step 3 wrap

then you can use the class 'no-wrap' for only the div's that do not wrap


Try this :)

$('div:not(#dontselect *)')


Another into the mix:

$('div').filter(function(){ return !$(this).parents('#dontselect').length })

Filters out all elements with the #dontselect parent.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜