私は自分のjQueryスライドデジグルを持っていて、それは最初の<div>
のために機能しますが、もっと多くのフリップやパネルを追加するとうまくいきません。それはthisのようにする必要があります。 上記をクリックすると動作します。第二のものは反応しない。どうすれば修正できますか?jQuery Slidetoggleを複数回使用するにはどうすればよいですか?
フルコード:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js
</script>
<script>
$(document).ready(function(){
$("#flip").click(function(){
$("#panel").slideToggle("slow");
});
});
$(document).ready(function(){
$("#flip1").click(function(){
$("#panel1").slideToggle("slow");
});
});
</script>
<style>
#panel, #flip {
padding: 5px;
text-align: center;
background-color: #e5eecc;
border: solid 1px #c3c3c3;
}
#panel {
padding: 50px;
display: none;
}
#panel1, #flip1 {
padding: 5px;
text-align: center;
background-color: #e5eecc;
border: solid 1px #c3c3c3;
}
#panel1 {
padding: 50px;
display: none;
}
</style>
</head>
<body>
<div id="flip">Click to slide the panel down or up</div>
<div id="panel">Hello world!</div>
<br><br>
<h1 style="text-align: center;">This example below won't work. Why not?</h1><br> <br>
<div id="flip">Click to slide the seconds panel down or up</div>
<div id="panel">Hello world!</div>
<br><br>
<h1 style="text-align: center;">To fix this I could try using different names for the flip and panel such as flip1 and panel1</h1><br><br>
<div id="flip1">Click to slide the panel down or up</div>
<div id="panel1">Hello world!</div>
</body>
</html>
要素のIDは一意である必要があり、使用することですクラスの代わりに、クリックハンドラで次の兄弟要素を切り替える必要があります –