[英]jQuery Accordion Panels that wont close
I'm new to jquery and I have the following code that creates accordion style panels from divs. 我是jquery新手,我有以下代码可从div创建手风琴样式面板。 The code runs fine, however, if I click on a panel that's already open, it closes the panel, and then instantly reopens it.
该代码运行良好,但是,如果我单击已经打开的面板,它将关闭该面板,然后立即重新打开它。 This only applies if its an already active panel.
仅当其面板已经激活时才适用。 If I click a different one it works fine.
如果我单击其他按钮,则效果很好。
$(document).ready(function() {
$('.accordion-section-title').addClass('active');
// Open up the hidden content panel
$('.accordion ' + '#accordion-1').slideDown(300).addClass('open');
function close_accordion_section() {
$('.accordion .accordion-section-title').removeClass('active');
$('.accordion .accordion-section-content').slideUp(300).removeClass('open');
}
$('.accordion-section-title').click(function(e) {
// Grab current anchor value
var currentAttrValue = $(this).attr('href');
if($(e.target).is('.active')) {
close_accordion_section();
}else {
close_accordion_section();
// Add active class to section title
$(this).addClass('active');
// Open up the hidden content panel
$('.accordion ' + currentAttrValue).slideDown(300).addClass('open');
}
e.preventDefault();
});
});
I've attached a js fiddle, It looks like the issue happens whenever I wrap the title in any tag, if its just blank text, it works fine. 我已经附上了一个js小提琴,看来只要将标题包装在任何标签中,只要标题为空白文本,就可以正常工作。
https://jsfiddle.net/russ1337/ynfs4zw3/ https://jsfiddle.net/russ1337/ynfs4zw3/
based on your fiddle, I have found the problem. 根据您的小提琴,我发现了问题。
Please see updated fiddle: 请参阅更新的小提琴:
https://jsfiddle.net/ynfs4zw3/2/ https://jsfiddle.net/ynfs4zw3/2/
The problem was the following code: 问题是以下代码:
if($(e.target).is('.active')) {
close_accordion_section();
}else {
...
}
Your if statement said that if e.target is active but in if you clicked directly on the text, the target was the inside the .accordion-section-title div. 您的if语句表示如果e.target是活动的,但是如果直接单击文本,则该目标为.accordion-section-title div的内部。 Which did not have the .active class.
其中没有.active类。
Try removing the close_accordion_section(); 尝试删除close_accordion_section(); in the else statement:
在else语句中:
$(document).ready(function() {
$('.accordion-section-title').addClass('active');
// Open up the hidden content panel
$('.accordion ' + '#accordion-1').slideDown(300).addClass('open');
function close_accordion_section() {
$('.accordion .accordion-section-title').removeClass('active');
$('.accordion .accordion-section-content').slideUp(300).removeClass('open');
}
$('.accordion-section-title').click(function(e) {
// Grab current anchor value
var currentAttrValue = $(this).attr('href');
if($(e.target).is('.active')) {
close_accordion_section();
}else {
// Add active class to section title
$(this).addClass('active');
// Open up the hidden content panel
$('.accordion ' + currentAttrValue).slideDown(300).addClass('open');
}
e.preventDefault();
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.