jQuery - Select everything except a single elements and its children?

后端 未结 6 1634
栀梦
栀梦 2020-12-10 03:47

I\'m trying to remove everything in the whole body, except of a single element and its children. How can I accomplish this?

Edit 1 Consider the foll

6条回答
  •  旧巷少年郎
    2020-12-10 04:10

    This is an old question, but the accepted answer is wrong. It doesn't work in this situation (see http://jsfiddle.net/LVb3V/ ) and certainly not as a general solution.

    Better to use:

    $('body *').not('#div2, #div2 *').remove()​;

    This will remove all elements in the body except for div2 and all the elements it contains.

提交回复
热议问题