我有我的 jQuery Slidetoggle,它适用于第一个 <div>
但当添加更多翻盖和面板时,它就不起作用了。它必须像 this 。
我点击上面的那个就可以了。第二个连 react 都没有。我该如何解决这个问题?
完整代码:
<!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 必须是唯一的,这里是 demo
$(document).ready(function(){
$("#flip").click(function(){
$("#panel").slideToggle("slow");
});
$("#flip1").click(function(){
$("#panel1").slideToggle("slow");
});
});
请使用jsfiddle如下better version :)
关于javascript - 如何多次使用 jQuery Slidetoggle?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36403473/