2012-08-31 30 views
0

我試圖實現這個jQuery例如:http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_slide_toggleHTML和jQuery的 - 上點擊開DIV

,但它僅與一個DIV工作。當我點擊它們時,如何讓它打開其他div。這就是我:

<script type="text/javascript"> 
$(document).ready(function(){ 
$(".flip").click(function(){ 
$(".panel").slideToggle("slow"); 
    }); 
}); 
</script> 
<style type="text/css"> 
div.panel, p.flip { 
margin: 0px; 
padding: 5px; 
text-align: center; 
background: #333; 
border: 1px solid #2C2C2C 
} 
div.panel { 
display: none; 
} 
</style> 

    <div id="content"> 
    <h2>Creating the scenario</h2> 
    <p>sample text sample text. <br /> 
    </p> 
    <p class="flip">[+] Code Hint #1</p> 
    <div class="panel" style="line-height:1.5px;"> 
    <p>Some hint related to code segment</p> 
    </div> 
</div> 
<br /> 
<br /> 
<div id="content"> 
    <h2>Adding the hero</h2> 
    <p>sample text sample text. <br /> 
    </p> 
    <p class="flip">[+] Code Hint #1</p> 
    <div class="panel" style="line-height:1.5px;"> 
    <p>Some hint related to code segment</p> 
    </div> 
</div> 
<br /> 
<br /> 
<div id="content"> 
    <h2>Moving with keyboard commands</h2> 
    <p>sample text sample text. <br /> 
    </p> 
    <p class="flip">[+] Code Hint #1</p> 
    <div id="panel1" class="panel" style="line-height:1.5px;"> 
    <p>Some hint related to code segment</p> 
    </div> 
</div> 
+2

您的HTML重複'id's,你可能要考慮清理這件事。 – Chad

回答

3

嘗試

$(".flip").click(function() { 
    $(this).next(".panel").slideToggle("slow"); 
});​ 

jsFiddle example

+0

感謝作品像一個魅力:) – user1639423

+0

沒問題,很樂意提供幫助。 – j08691

+0

@ user1639423如果這有幫助和工作,請將此標記爲解決方案/您的問題的答案。 –