我正在尝试通过jQuery自定义手风琴,但它不起作用。 有人可以帮帮我吗?
我的示例代码如下,让我知道是否会有任何问题。
提前致谢
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".accordion li h5").click(function () {
var current_li = $(this).parent();
$(".accordion li div").each(function(i,el) {
if($(el).parent().is(current_li)) {
$(el).prev().toggleClass("plus");
$(el).slideToggle();
} else{
$(el).prev().removeClass("plus");
$(el).slideUp();
}
});
});
$('.accordion li > div').hide();
$('.accordion li h5').first().addClass("plus");
$('.accordion li > div').first().show().addClass("plus");
});
</script>
.accordion ul{
border-bottom:#ecebe9 solid 1px;
margin:0;
padding:0;
font-family: Arial, Helvetica, sans-serif;
}
.accordion ul li{
border-left:#ecebe9 solid 1px;
border-right:#ecebe9 solid 1px;
list-style:none;
}
.accordion ul li h5{
font-size:12px;
color:#333333;
font-weight:bold;
background:#f7f6f5;
border-top:#ecebe9 solid 1px;
border-left:#ecebe9 solid 1px;
border-right:#ecebe9 solid 1px;
padding:12px 15px 12px 40px;
line-height:19px;
cursor:pointer;
position:relative;
}
.accordion ul li h5.plus{
border-bottom:#ecebe9 solid 1px;
}
.accordion ul li > div{
font-size:12px;
color:#333333;
line-height:20px;
padding:15px 15px 15px 40px;
}
.accordion ul li > div a{
color:#7f0a19;
}
.accordion ul li h5:before{
content:"";
background:#f00;
width:11px;
height:11px;
opacity:0.7;
position:absolute;
left:14px;
top:14px;
transition: all 0.3s ease-in-out 0s;
-webkit-transition: all 0.3s ease-in-out 0s;
-o-transition: all 0.3s ease-in-out 0s;
}
.accordion ul li h5.plus:before{
opacity:1;
}
<div class="faq">
<div class="accordion">
<ul>
<li>
<h5>Title Lorem Ipsum is simply dummy text of the printing</h5>
<div>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the</div>
</li>
<li>
<h5>Title Lorem Ipsum is simply dummy text of the printing</h5>
<div>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the</div>
</li>
</ul>
</div>
</div>
答案 0 :(得分:1)
您的代码确实有效。 Here is the JSFiddle及以下是您发布的代码段。
将jquery库添加到代码段的方式中只有错误。您应该添加外部源(链接到jquery),或使用<script>
标记,而不是两者。
$(document).ready(function () {
$(".accordion li h5").click(function () {
var current_li = $(this).parent();
$(".accordion li div").each(function (i, el) {
if ($(el).parent().is(current_li)) {
$(el).prev().toggleClass("plus");
$(el).slideToggle("fast");
} else {
$(el).prev().removeClass("plus");
$(el).slideUp("fast");
}
});
});
$('.accordion li > div').hide();
$('.accordion li h5').first().addClass("plus");
$('.accordion li > div').first().show().addClass("plus");
});
.accordion ul {
border-bottom:#ecebe9 solid 1px;
margin:0;
padding:0;
font-family: Arial, Helvetica, sans-serif;
}
.accordion ul li {
border-left:#ecebe9 solid 1px;
border-right:#ecebe9 solid 1px;
list-style:none;
}
.accordion ul li h5 {
font-size:12px;
color:#333333;
font-weight:bold;
background:#f7f6f5;
border-top:#ecebe9 solid 1px;
border-left:#ecebe9 solid 1px;
border-right:#ecebe9 solid 1px;
padding:12px 15px 12px 40px;
line-height:19px;
cursor:pointer;
position:relative;
}
.accordion ul li h5.plus {
border-bottom:#ecebe9 solid 1px;
}
.accordion ul li > div {
font-size:12px;
color:#333333;
line-height:20px;
padding:15px 15px 15px 40px;
}
.accordion ul li > div a {
color:#7f0a19;
}
.accordion ul li h5:before {
content:"";
background:#f00;
width:11px;
height:11px;
opacity:0.7;
position:absolute;
left:14px;
top:14px;
transition: all 0.3s ease-in-out 0s;
-webkit-transition: all 0.3s ease-in-out 0s;
-o-transition: all 0.3s ease-in-out 0s;
}
.accordion ul li h5.plus:before {
opacity:1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<div class="faq">
<div class="accordion">
<ul>
<li>
<h5>Title Lorem Ipsum is simply dummy text of the printing</h5>
<div>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the</div>
</li>
<li>
<h5>Title Lorem Ipsum is simply dummy text of the printing</h5>
<div>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the</div>
</li>
</ul>
</div>
</div>
答案 1 :(得分:0)
尝试:
<div class="faq">
<div class="accordion">
<ul>
<li>
<h5 class="header">Title Lorem Ipsum is simply dummy text of the printing</h5>
<div class="content">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the</div>
</li>
<li>
<h5 class="header">Title Lorem Ipsum is simply dummy text of the printing</h5>
<div class="content">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the</div>
</li>
</ul>
</div>
</div>
jQuery的:
function MyAccordion() {
$('.accordion .content').hide();
$('.accordion .header').click(function() {
if ($(this).hasClass("active")) {
$(this).nextUntil('.header').slideUp(250);
$(this).removeClass('active');
} else {
$(this).nextUntil('.header').slideDown(250);
$(this).siblings('.header').nextUntil('.header').slideUp(250);
$(this).addClass('active');
$('.accordion .header').not(this).removeClass('active');
}
});
}
MyAccordion();
为&#34;有效&#34; 类添加减号。该脚本将活动类添加到开放式手风琴乐段上方的<h5>
标签。
JsFiddle HERE
答案 2 :(得分:0)
或者,如果您希望单击“标题”不仅打开其下方的内容,而是关闭其他内容,请使用:
function MyAccordion() {
$('.accordion .content').hide();
$('.accordion .header').click(function() {
if ($(this).hasClass("active")) {
$(this).nextUntil('.header').slideUp(250);
$(this).removeClass('active');
} else {
$('.accordion .header').removeClass('active');
$('.accordion .content').slideUp(250);
$(this).nextUntil('.header').slideDown(250);
$(this).siblings('.header').nextUntil('.header').slideUp(250);
$(this).addClass('active');
$('.accordion .header').not(this).removeClass('active');
}
});
}
MyAccordion();
.accordion ul {
border-bottom:#ecebe9 solid 1px;
margin:0;
padding:0;
font-family: Arial, Helvetica, sans-serif;
}
.accordion ul li {
border-left:#ecebe9 solid 1px;
border-right:#ecebe9 solid 1px;
list-style:none;
}
.accordion ul li h5 {
font-size:12px;
color:#333333;
font-weight:bold;
background:#f7f6f5;
border-top:#ecebe9 solid 1px;
border-left:#ecebe9 solid 1px;
border-right:#ecebe9 solid 1px;
padding:12px 15px 12px 40px;
line-height:19px;
cursor:pointer;
position:relative;
}
.accordion ul li h5.active {
border-bottom:#ecebe9 solid 1px;
}
.accordion ul li > div {
font-size:12px;
color:#333333;
line-height:20px;
padding:15px 15px 15px 40px;
}
.accordion ul li > div a {
color:#7f0a19;
}
.accordion ul li h5:before {
content:"";
background:#f00;
width:11px;
height:11px;
opacity:0.7;
position:absolute;
left:14px;
top:14px;
transition: all 0.3s ease-in-out 0s;
-webkit-transition: all 0.3s ease-in-out 0s;
-o-transition: all 0.3s ease-in-out 0s;
}
.accordion ul li h5.active:before {
opacity:1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="faq">
<div class="accordion">
<ul>
<li>
<h5 class="header">Title Lorem Ipsum is simply dummy text of the printing</h5>
<div class="content">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the</div>
</li>
<li>
<h5 class="header">Title Lorem Ipsum is simply dummy text of the printing</h5>
<div class="content">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the</div>
</li>
<li>
<h5 class="header">Title Lorem Ipsum is simply dummy text of the printing</h5>
<div class="content">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the</div>
</li>
<li>
<h5 class="header">Title Lorem Ipsum is simply dummy text of the printing</h5>
<div class="content">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the</div>
</li>
</ul>
</div>
</div>