开发者

jquery - hide all div except one class

Here is my code :

  <div id="principal" class="classxx">
    <div class="class001 cat2 blabla">
      <div class="class002">
        <div class="class003"></div>
        <div class="class004"></div>
        <div class="class005"></div>
      </div>
    </div>
    <div class="class001 cat3 blabla">
      <div class="class002">
        <div class="class003"></div>
        <div class="class004"></div>
        <div class="class005"></div>
      </div>
    </div>
    <div class="class001 cat1 blabla">
      <div class="class002">
        <div class="class003"></div>
        <div class="class004"></div>
        <div class="class005"></div>
      </div>
    </div>
  </div>

I need开发者_如何学JAVA a function like :

function showOnlyCat(className){}

If I call showOnlyCat('cat3') I want to see only all the DIVs that have the 'cat3' class (each DIV has multiple classes) and its children of course

And I also need a showAllCat() that shows all the cat1, cat2, cat3... DIVs

Thank you VERY MUCH for your help


try this :

function showOnlyCat(cat) {
    $('div').not('.'+cat).hide();
}
function showAllCat() {
    $('div').show();
}


$('div.cat').siblings().show();

And all siblings with the above will be shown.


function showOnlyCat(cat){

 $('.class001').hide();
 $('.'+ cat).show();
}

function showAllCat(){
  $('.class001').show();
}

That's it when class001 is right.


Can be achieved with one function really

function toggleVisibility(className){
     if(typeof(className)!='undefined' && className != null){
        $("div").hide();         
        $("div."+className).show();
     }
   else
        $("div").show();
}

//to only show class001 toggleVisibilty('class001'); //to show all toggleVisibility();


function showOnlyCat(className) {
  $('div').not('.' + className).hide();
}

function showAllCat() {
  for (i=1;i<=NUM_CAT;i++) {
    $('.cat' + i).show();
  }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜