How to hide all elements except one using jquery?

后端 未结 4 1744
一生所求
一生所求 2020-12-02 08:31

I have HTML page:



  
&
4条回答
  •  一个人的身影
    2020-12-02 09:06

    This should work:

    $('div:not(#myDiv)').hide();  // hide everything that isn't #myDiv
    $('#myDiv').appendTo('body');  // move #myDiv up to the body
    

    Update:

    If you want to hide EVERYTHING that, not just div elements, use this instead:

    $('body > :not(#myDiv)').hide(); //hide all nodes directly under the body
    $('#myDiv').appendTo('body');
    

    Probably simpler is to wrap the entire "hideable" part of the page in a big container element, and hide that directly though.

    Like so:

     
         
提交回复
热议问题