2014-09-02 52 views
-1

我有這樣的HTML代碼隱藏內容:jQuery的 - 在文章

<article class="about"> 
      <header> 
       <div class="hed"></div> 
      </header> 

      <div class="contentt"><content> 

      </content></div> 
</article> 

和這個js:

$(".hed").unbind("click").click(function(){ 
     $(".contentt", this).hide(); 
    } 

我需要隱藏類contentt,當我點擊類的建置,但我有更多的文章,所以我需要使用類來隱藏它。一些想法?

回答

2

.contentt不是.hed後代,所以你的方法是行不通的。這是更好的辦法:

$('.hed').unbind('click').click(function() { 
    $(this).closest('article').find('.contentt').hide(); 
}); 

這裏腳本獲取最接近article父母找到.contentt塊在它的後代。

+0

好的,謝謝你這麼多。 :) – 2014-09-02 16:44:30