0

我正在尝试使用手风琴和带有 jQ​​uery 插件哈希更改的插件来引导 Twitter,如果使用 location.hash 打开页面并多次单击其他项目,则在项目无法切换关闭并保持打开之前。

有好的解决方法,请帮忙谢谢

使用 hashchange 标签:#collapseOne、#collapseTwo、#collapseThree、

问题

  • 打开项目
  • 打开项目
  • 项目关闭

html

    <div class="row">
    <div class="span32">
        <div class="accordion" id="accordion2">
            <div class="accordion-group">
                <div class="accordion-heading">
                    <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseOne">Item #1</a>
                </div>
                <div id="collapseOne" class="accordion-body collapse">
                    <div class="accordion-inner">content1</div>
                </div>
            </div>
            <div class="accordion-group">
                <div class="accordion-heading">
                    <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseTwo">#2</a>
                </div>
                <div id="collapseTwo" class="accordion-body collapse">
                    <div class="accordion-inner">content2</div>
                </div>
            </div>
            <div class="accordion-group">
                <div class="accordion-heading">
                    <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseThree">#3</a>
                </div>
            </div>
        </div>
    </div>
</div>

js

    $(function () {

        $(window).hashchange(function () {
            var hash = location.hash;
            var action = hash.replace('#', '');

            $('.accordion-heading a').click(function () {
                window.location.hash = $(this).attr("href");
                console.log('--------');
                console.log(location.pathname);
                console.log(hash);
            });

            switch (action) {
                case "collapseOne":
                    $("#collapseOne").collapse('toggle');
                    break;
                case "collapseTwo":
                    $("#collapseTwo").collapse('toggle');
                    break;
                case "collapseThree":
                    $("#collapseThree").collapse('toggle');
                    break;
                default:

                    break;
            };
        });
        $(window).hashchange();
    });

这是演示

这是代码

4

2 回答 2

1

试试这个(哈希历史的更新代码):

$('.accordion-heading a').click(function(e) {
    var hash = $(this).attr('href');
    document.title = 'The hash is ' + (hash.replace('#', '') || 'blank') + '.';
    location.hash = hash;
});

jsFiddle

于 2013-07-16T10:57:22.710 回答
0

你想要这样的小提琴

$('.accordion-toggle').on('click',function(e){
if($(this).parents('.accordion-group').children('.accordion-body').hasClass('in')){
    e.stopPropagation();
}
});
于 2013-07-16T10:43:56.377 回答