我有这段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/。
$(".tablink").click(function(){
中有一个拼写错误,您在.tablink
上使用了小写字母L
。 - user13500window.hash
更改的事件感兴趣吗?请看下面我的新回答... - SoAwesomeMan