2

我使用了来自http://w3schools.com的以下代码。但在这个我只能使用一次。我想多次使用它,因为我想在 HTML 中创建常见问题解答面板,但它来自 ID,我只能使用一次。

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script> 
$(document).ready(function(){
  $("#flip").click(function(){
    $("#panel").slideToggle("slow");
  });
});
</script>

<style type="text/css"> 
#panel,#flip
{
padding:5px;
text-align:center;
background-color:#e5eecc;
border:solid 1px #c3c3c3;
}
#panel
{
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>

</body>
</html>
4

2 回答 2

6

演示

脚本:

$(function(){
    $('a.toggle').click(function(){
        $('.myContent').stop().slideToggle(500);
        return false;
    });

});

HTML

<a href="#" style="background-color: #99CCFF; padding: 5px 10px;" class="toggle">CLICK</a>

<div>
    <div class="myContent" style="background-color: #99CCFF; padding: 5px 10px;">Optimized the javascript so that all code is based on jQuery.
    </div>

    <div class="myContent" style="background-color: #CCCCFF; padding: 5px 10px;">Optimized the javascript so that all code is based on jQuery.
    </div>
</div>
于 2013-11-01T07:49:11.890 回答
3

更改idclass然后在 css 和 jQuery 中使用类选择器

<div class="flip">Click to slide the panel down or up</div>
<div class="panel">Hello world!</div>
<div class="flip">Click to slide the panel down or up</div>
<div class="panel">Hello world!</div>
<div class="flip">Click to slide the panel down or up</div>
<div class="panel">Hello world!</div>

CSS

.panel, .flip {
    padding:5px;
    text-align:center;
    background-color:#e5eecc;
    border:solid 1px #c3c3c3;
}
.panel {
    padding:50px;
    display:none;
}

然后

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

演示:小提琴

于 2013-11-01T07:49:30.830 回答