2010-08-05 45 views
6

我怎麼能使用相鄰的選擇器「+」與$(this)。

我需要用//這不工作的註釋行幫助:

$(".ExpandCollapse").click(function() { 
      if ($(this).nextUntil('.Collapsable').is(':visible')) 
      { 
       //this doesnt work 
       $(this + ".Collapsable").hide(); 
      } 
      else 
      { 
       //this doesnt work 
       $(this + ".Collapsable").show(); 
      } 
     }); 

能給我一個忙嗎?

非常感謝。

最好的問候。

何塞

回答

9

使用next()

$(this).next(".Collapsable").hide(); 

或者乾脆:

$(this).next().hide(); 
2

您還可以減少在具有隱蔽性兩個語句,並顯示:

$(this).next().toggle(); 
1

this是對調用的DOM element的引用。你不能連接string

所以你要麼可以直接使用this採取行動,那麼

$(this).hide(); 

,或者您可以通過DOM從那裏

$(this).next().hide(); 
$(this).prev().hide(); 
$(this).closest('.Collapsable').hide(); 
// another 200 methods