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

后端 未结 6 1636
栀梦
栀梦 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:15

    I think that

    $("body *").not("body #div2")
               .not("body #div2 #elementtokeep")
               .not("body #div2 #elementtokeep *")
               .remove();
    

    is the easiest way to focus it.

    http://jsfiddle.net/6vb2va6g/

提交回复
热议问题