2015-11-03 81 views
0

我試圖顯示和隱藏網頁的某些部分,而不顯示或隱藏其他具有類似名稱的元素,例如我想能夠顯示某些評論回覆而不會顯示具有相同類名的其他評論回覆。但我似乎不能讓我的jquery工作正確。換句話說,我想單擊類view-comments中的一個元素,並顯示類名最接近的部分more-comments顯示一個隱藏和顯示部分一次使用jquery

這是我簡化的HTML代碼。

HTML

 <article> 
      <div> 
       <div> 
        .... 
        <a class="view-comments" title="" href="">View Comments</a> 
       </div> 
      </div> 

      <section class="more-comments"> 
       ....... 
      </section> 
     </article> 

     <article> 
      <div> 
       <div> 
        .... 
        <a class="view-comments" title="" href="">View Comments</a> 
       </div> 
      </div> 

      <section class="more-comments"> 
       ....... 
      </section> 
     </article> 


     <article> 
      <div> 
       <div> 
        .... 
        <a class="view-comments" title="" href="">View Comments</a> 
       </div> 
      </div> 

      <section class="more-comments"> 
       ....... 
      </section> 
     </article> 

     <article> 
      <div> 
       <div> 
        .... 
        <a class="view-comments" title="" href="">View Comments</a> 
       </div> 
      </div> 

      <section class="more-comments"> 
       ....... 
      </section> 
     </article> 

jQuery的

$(document).ready(function() { 
     $('.view-comments').click(function() { 
     $('.more-comments').toggle($(this).hasClass('view-comments')); 
     $(this).toggleClass('view-comments hide-comments'); 
     }); 
    }); 
+0

嘗試http://jsfiddle.net/a8zbuj80/1這是你想要的 – guradio

回答

1

要做到這一點,我會做一切不具有默認隱藏的類active元素。然後,當你點擊一個鏈接(稱爲防止默認阻止頁面導航),從所有more-comments部分刪除類active,然後添加類active到最近more-comments部分

$(document).ready(function() { 
 
     $('.view-comments').click(function(e) { 
 
     e.preventDefault(); 
 
     $('.more-comments').removeClass('active'); 
 
     $(this).closest('article').find('.more-comments').addClass('active'); 
 
     }); 
 
    });
.more-comments:not(.active){ 
 
    display:none; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<article> 
 
      <div> 
 
       <div> 
 
        .... 
 
        <a class="view-comments " title="" href="">View Comments</a> 
 
       </div> 
 
      </div> 
 

 
      <section class="more-comments active"> 
 
       .......more-comments 1 
 
      </section> 
 
     </article> 
 

 
     <article> 
 
      <div> 
 
       <div> 
 
        .... 
 
        <a class="view-comments" title="" href="">View Comments</a> 
 
       </div> 
 
      </div> 
 

 
      <section class="more-comments"> 
 
       ....... more-comments 2 
 
      </section> 
 
     </article> 
 

 

 
     <article> 
 
      <div> 
 
       <div> 
 
        .... 
 
        <a class="view-comments" title="" href="">View Comments</a> 
 
       </div> 
 
      </div> 
 

 
      <section class="more-comments"> 
 
       ....... more-comments 3 
 
      </section> 
 
     </article> 
 

 
     <article> 
 
      <div> 
 
       <div> 
 
        .... 
 
        <a class="view-comments" title="" href="">View Comments</a> 
 
       </div> 
 
      </div> 
 

 
      <section class="more-comments"> 
 
       ....... more-comments 4 
 
      </section> 
 
     </article>

+0

請問什麼e.preventDefault();是什麼,如果我刪除它實際上做了什麼?謝謝。 – gchow

+0

您的'view-comments'元素是鏈接,默認情況下,鏈接導航到存儲在'href'屬性中的地址。當你舔這樣的鏈接時,它會在綁定的jQuery函數被調用之前(或者至少在查看結果之前)導航到它的href中的頁面。 'e.preventDefault();'防止發生這種情況 – DelightedD0D

+0

請注意,這裏假設您只希望一個部分一次可見(通常需要),但是如果您希望能夠同時打開多個部分,使用@ Pekka的回答 – DelightedD0D

1
$('.view-comments').click(function (e) { 
    e.preventDefault(); 
    $(this).closest('article').find('.more-comments').toggle(); 
}); 

demo

嘗試這種方式

+1

個人而言,我更喜歡'$(this).closest('article')。find('。more-comments')。toggle();'因爲它不太可能打破' html結構。但是+1 :) – DelightedD0D

+1

請致電@ DelightedD0D包括回答爲更新。或者如果你想我可以爲你做。謝謝你的建議 – guradio