我真的坚持使用我的代码,它工作正常除了一件事,我需要关闭任何打开的div,当我点击其他div打开时,这是我的代码:
function readFaq() {
$('.find-faq-inner h3').prepend('<span id="spanfaq" class="faq-open"></span>');
$('.find-faq-inner h3').each(function () {
var tis = $(this), state = false;
var answer = tis.next('div').hide().css('height', 'auto').slideUp();
tis.click(function () {
state = !state;
answer.slideToggle(state);
tis.toggleClass('active', state);
if (state == true) {
tis.find('#spanfaq').removeClass('faq-open');
tis.find('#spanfaq').addClass('faq-close');
}
else {
tis.find('#spanfaq').removeClass('faq-close');
tis.find('#spanfaq').addClass('faq-open');
}
});
});
}
<div class="find-faq-inner">
<div class="text">
<h3>FAQ1</h3>
<div>
text of faq
</div>
</div>
</div>
任何想法都会有所帮助,谢谢
答案 0 :(得分:1)
function readFaq() {
$('.find-faq-inner h3').prepend('<span id="spanfaq" class="faq-open"></span>');
$('.find-faq-inner h3').each(function () {
var tis = $(this), state = false, answer = tis.next('div').hide().css('height', 'auto').slideUp();
tis.click(function () {
state = !state;
$('.find-faq-inner .text div').hide();// This will hide all the divs
answer.slideToggle(state);
tis.toggleClass('active', state);
if (state == true) {
tis.find('#spanfaq').removeClass('faq-open');
tis.find('#spanfaq').addClass('faq-close');
}
else {
tis.find('#spanfaq').removeClass('faq-close');
tis.find('#spanfaq').addClass('faq-open');
}
});
});
}
在上面添加的功能中
$('.find-faq-inner .text div').hide();
这将隐藏find-faq-inner
div中的所有div,并在您的代码中相应地使用它。
为了更安全,您还可以使用
$('.find-faq-inner .text > div').hide();
它只会隐藏那些是类text