如何保持Bootstrap手风琴保持一个打开,使用hashchange事件,点击事件



我正在尝试使用手风琴和插件进行Twitter引导,并更改jQuery插件的哈希值,如果使用location.hash打开页面并点击其他项目多次,之前项目无法切换关闭并保持打开

有很好的解决方案,请帮忙,谢谢

使用hashchange标签:#collapseOne,#collapseTwo,#collapse3,

发布

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

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

这是演示

这里的代码

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

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

jsFiddle

你想要这样的小提琴

$('.accordion-toggle').on('click',function(e){
if($(this).parents('.accordion-group').children('.accordion-body').hasClass('in')){
    e.stopPropagation();
}
});

相关内容

  • 没有找到相关文章

最新更新