2013-04-12 22 views
0

此時此功能隱藏所有顯示的答案。我需要它只隱藏屬於第二次點擊的特定問題的答案。我對JQuery非常陌生,所以可能有一個簡單的解決方案,但任何幫助都會很棒。只需要隱藏特定的'.answer'當任務。再次點擊該答案

$(document).ready(function() { 
     $(".question").click(function() { 
      $('.answer').hide(); // hides open answers before showing a new answer 
     if ($(this).next().is(":hidden")) { // check the visibility of the next element in the DOM 
      $(this).next().show(); // show it 
     } else { 
      $(this).next().hide(); // hide it 
     } 
    }); 
}); 
+2

請過帳html。這將有助於! –

+2

把html也放在這裏或創建jsfiddle – rahularyansharma

+0

所以你需要每個問題的計數器!並且您必須將其存儲爲檢查問題的標誌。如果計數器> 1隱藏....這樣的東西http://forum.jquery.com/topic/how-to-make-a-button-disabled-after-3-clicks – zod

回答

2

既然沒有HTML走下車的,我嘲笑了一個例子here

HTML

<span class="question">What is your name?</span> 
<span class="answer">Chase</span> 
<br/> 
<label class="question">How old are you?</label> 
<span class="answer">25</span> 
<br/> 
<label class="question">What is your favorite color?</label> 
<span class="answer">Blue</span> 
<br/> 
<label class="question">Do you like cheese?</label> 
<span class="answer">Duh</span> 

的JavaScript/jQuery的

$(document).ready(function() { 
    $(".question").click(function() { 
    $(this).next("span.answer").fadeToggle(); 
    }); 
}); 

我正在使用next方法得到以下.answerclick ed question

+0

這就像一個夢想工作!非常感謝你 – Lars

+0

很高興聽到它。如果您不介意,請繼續並將其標記爲答案=) – Chase

相關問題