2015-02-24 58 views
0

我無法弄清楚如何添加關閉其他divs的功能時,當我點擊一個div新div與相同的類。我知道它應該很簡單,但不能讓它工作。這裏是小提琴 - http://jsfiddle.net/dnym5p1s/隱藏其他Divs當點擊新的Div與相同的類

added correct link 

<div class="option-heading"> 
      <div class="arrow-up">&#9650;</div> 
      <div class="arrow-down">&#9660;</div> 
      <h1>Year1</h1> 
     </div> 
     <div class="option-content"> 
      Content 
     </div> 
     <div class="option-heading"> 
      <div class="arrow-up">&#9650;</div> 
      <div class="arrow-down">&#9660;</div> 
      <h1>Year2</h1> 
     </div> 
     <div class="option-content"> 
      Content 
     </div> 
$(".option-content").hide(); $(".arrow-up").hide(); 
     $(".option-heading").click(function(){ 
       $(this).next(".option-content").slideToggle(150); 
       $(this).find(".arrow-up, .arrow-down").toggle(); 
     }); 
+0

始終在您的問題中發佈您的代碼。像jsFiddle這樣的網站應該是對此的恭維,而不是代替它。 – j08691 2015-02-24 16:12:21

+0

你可以指定更多你想要完成的事情嗎? – Djip 2015-02-24 16:18:08

回答

2

這將做到這一點:

$(".option-content,.arrow-up").hide(); 
$(".option-heading").click(function() { 
    $(".option-content").not($(this).next()).slideUp(250); 
    $(".option-heading").not(this).find("div.arrow-up").hide(); 
    $(".option-heading").not(this).find("div.arrow-down").show(); 
    $(this).next(".option-content").slideToggle(250); 
    $(this).find(".arrow-up, .arrow-down").toggle(); 
}); 

jsFiddle example

0

剛上點擊監聽器的頂部添加$(".option-content").hide();像這樣:

$(".option-heading").click(function(){ 
    $(".option-content").hide(); 
    $(this).next(".option-content").slideToggle(150); 
    $(this).find(".arrow-up, .arrow-down").toggle(); 
}); 
+0

補充 - 我知道這很容易 - 只是讓它複雜化。 – RooksStrife 2015-02-24 16:19:33

+0

除了一旦你打開一個項目,你不能用這個代碼關閉它。 – j08691 2015-02-24 16:20:10

相關問題