我正在尝试使用手风琴和带有 jQuery 插件哈希更改的插件来引导 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();
});
这是演示
这是代码