2014-10-18 53 views
0

我把這個jQuery放在一起,當文檔準備好時divbio.read_more類被隱藏。約束切換效果

a標籤的默認行爲與e.preventDefault

刪除當btn.expand_bio點擊時顯示bio.read_more

唯一的問題是全部bio.read_more divs顯示?我只想要與之相關的擴展。誰能幫忙?

$(document).ready(function(){ 

    $(".bio.read_more").hide(); 

    $(".btn.expand_bio").click(function(e){ 
    e.preventDefault(); 
    $(".bio.read_more").toggle(); 
    }); 

}); 

(更新)添加了HTML

<div class="bio intro"> 
    <p>Intro paragraph</p> 
    <a class="btn expand_bio" href="#">Read more</a> 
    </div> 
    <div class="bio read_more"> 
    <p>Expanded text here</p> 
    </div> 
    </div> 

回答

1

使用this作爲參考元件被點擊。

$(document).ready(function() { 
    $(".bio.read_more").hide(); 
    $(".btn.expand_bio").click(function(e) { 
     e.preventDefault(); 
     $(this).closest('.bio.intro').next('.read_more').toggle(); 
    }); 
}); 
+0

很快!我認爲解決方案可能與使用'$(this)'有關,但我不知道如何實現它...仍然不是如果我是誠實的。 – StephenMeehan 2014-10-18 16:04:19

+0

@StephenMeehan檢查編輯 – 2014-10-18 16:09:22

+0

作品非常感謝,我明白這是如何工作。感謝您看看這個,非常感謝。 – StephenMeehan 2014-10-18 16:12:04