2014-05-15 61 views
1

我有一個程序被配置爲當單擊+/-圖標時隱藏/顯示錶格行。該功能正在工作,但是,當父類別切換關閉時,我需要找出重置所有隱藏/顯示圖標的方法。Jquery - 如果父類被點擊切換圖像Src隱藏/顯示

$(function(){ 
//src vars 
var hide_src = "http://www.synchronizeddesigns.com/filter_hide.gif", 
    reveal_src = "http://www.synchronizeddesigns.com/filter_reveal.gif", 
     s = ''; 
//hide all sublevel elements 
$(".subsub, .subsubsub").hide(); 

$("a").click(function(e){ 
    e.preventDefault(); 
    var tID = e.target.id, 
     tClass = '.' + tID.replace('HS', ''); 

    $(tClass).toggle(); 

    if(!$(tClass).is(':visible')){ 
     s = hide_src; 

     //for each subcategory 
     $(tClass).each(function(){ 
      //get class names into classes array 
      var classes = $(this).attr('class').split(' '), 
       parentClass = ''; 

      //search classes array for class that begins with 'cat' 
      for (var j=0; j<classes.length; j++) { 
       if (classes[j].match("cat")){ 
        parentClass = classes[j]; 
       } 
      } 

      //find subsubsub elements that have a class that begins with 'cat#sub' 
      var subs = $('[class*=' + parentClass + 'sub]'); 

      //if there are sub elements, hide them too 
      if(subs){ 
       subs.hide(); 

       /***************************************************** 
       NEED HELP HERE !!!!!!!!!! 
       Need a way to reset all hide/show images icon 
       when 'parentClass' hide/show is clicked to close. 
       *****************************************************/ 

      } 
     }); 
    } else { 
     s = reveal_src; 
    } 
    //Change image src 
    $("#" + tID).attr('src', s); 
}); 
}); 

要複製:切換打開的所有家長和潛艇,然後關閉父母中的一方,然後重新打開父。你會發現,在+/-圖標保持之前的狀態

jsFiddle link

回答

0

您可以找到當前下的子類別的img節點,然後改變自己的src ATTR。

我已經更新您的jsfiddle:http://jsfiddle.net/nTyWv/12/

的代碼可能是這樣的:

  if(subs){ 
       innerIcons = jQuery.find("a > img[id*="+tID+"sub]"); 
       if (innerIcons) { 
        $(innerIcons).attr('src', s); 
       }; 
       subs.hide(); 
      } 
+0

正是我需要的!謝謝@Roberto – patlxix9