【发布时间】:2014-03-05 23:47:58
【问题描述】:
我有这个 jquery 代码:
$(document).ready(function() {
$(".tabLink").each(function(){
if(location.hash) {
$(".tabLink").removeClass("activeLink");
$(location.hash+"-1").addClass("activeLink");
$(".tabcontent").addClass("hide")
$(location.hash+"-1").removeClass("hide")
} else {
$(".tablink").click(function(){
$(".tabLink").removeClass("activeLink");
$(this).addClass("activeLink");
$(".tabcontent").addClass("hide")
$(location.hash+"-1").removeClass("hide")
});
}
});
});
要在标签之间切换,我的 html 是:
<a href="#companyinfo" class="tabLink activeLink">Company</a>
<a href="#contacts" class="tabLink">Contacts</a>
<div class="tabcontent" id="companyinfo-1">
</div>
<div class="tabcontent" id="contacts-1">
</div>
当我选择另一个选项卡时,我必须单击它两次才能使 div 显示
这里有一个完整的代码:http://jsfiddle.net/2SRZE/
【问题讨论】:
-
Ps.:
$(".tablink").click(function(){有错字,.tablink上有小写的L。 -
@user13500 很好发现但仍然一无所获 - jsfiddle.net/2SRZE/7
-
@user3223205 仍然对仅基于
window.hash更改的事件感兴趣?请参阅下面的新答案...
标签: javascript jquery html