2

我真的坚持我的代码,它工作正常,除了一件事,我需要关闭所有打开的 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>

任何想法都会有所帮助,谢谢

4

1 回答 1

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();

这将隐藏 div 中的所有find-faq-innerdiv,在您的代码中相应地使用它。

为了更安全,您还可以使用

$('.find-faq-inner .text > div').hide();

它只会隐藏那些是类 div 的直接子级的 divtext

于 2012-05-22T16:46:25.920 回答