JavaScript选项卡式界面-如何链接和标记选项卡?

JavaScript选项卡式界面-如何链接和标记选项卡?,javascript,jquery,tabs,Javascript,Jquery,Tabs,我已经用jQuery创建了一个选项卡式界面来显示/隐藏内容 我希望能够链接到一个特定的选项卡,并允许用户将当前的选项卡添加到书签中 当我为每个包含div的文件使用id时,我可以通过删除returnfalse来实现这一点,但这会导致页面跳到选项卡的包含div 有没有办法确保URL包含地址的#部分,但防止页面跳过?有没有其他方法来解决这个问题,我还没有遇到 //Get all containers with tab content var tabContainers = $("div.tab");

我已经用jQuery创建了一个选项卡式界面来显示/隐藏内容

我希望能够链接到一个特定的选项卡,并允许用户将当前的选项卡添加到书签中

当我为每个包含div的文件使用id时,我可以通过删除
returnfalse来实现这一点,但这会导致页面跳到选项卡的包含div

有没有办法确保URL包含地址的#部分,但防止页面跳过?有没有其他方法来解决这个问题,我还没有遇到

//Get all containers with tab content
var tabContainers = $("div.tab");  

//Get value of # from URL
if (window.location.hash) {

  //if there's a # display the relevant tab
  $(tabContainers).hide().filter(window.location.hash).show();  

} else {
    //Show the first tab
    $(tabContainers).hide().filter(":first").show();    
}


$("ul#tabNav a").click( function () {

    //Hide all tab content then display the current
    $(tabContainers).hide().filter(this.hash).show();

    //prevent page from skipping but also prevents # from appearing in address bar
    return false;

});





<div id="tabNavContainer">
    <ul id="tabNav">
        <li id="tab1"> <a href="#a">Course essentials</a> </li>
        <li id="tab2"> <a href="#b">Course details</a> </li>
        <li id="tab3"> <a href="#c">Next steps</a> </li>
    </ul>
</div>
<div class="tab" id="a">
    <h3>TAB A</h3>
</div>
<div class="tab" id="b">
    <h3>TAB B</h3>
</div>
<div class="tab" id="c">
    <h3>TAB C</h3>
</div>
//获取包含选项卡内容的所有容器
var tabContainers=$(“div.tab”);
//从URL获取#的值
if(window.location.hash){
//如果有#显示相关选项卡
$(tabContainers.hide().filter(window.location.hash).show();
}否则{
//显示第一个选项卡
$(tabContainers.hide().filter(“:first”).show();
}
$(“ul#tabNav a”)。单击(函数(){
//隐藏所有选项卡内容,然后显示当前选项卡
$(tabContainers.hide().filter(this.hash.show());
//防止页面跳过,但也防止#出现在地址栏中
返回false;
});
表A 表B 表C

非常感谢您的帮助。

您可以设置
窗口.位置.散列以及阅读:

$("ul#tabNav a").click( function () {

    //Hide all tab content then display the current
    $(tabContainers).hide().filter(this.hash).show();

    window.location.hash = this.hash;

    //prevent page from skipping but also prevents # from appearing in address bar
    return false;

});

请注意,这将在浏览器历史记录中添加一个页面,允许用户使用后退和前进按钮更改哈希。理想情况下,您还应该考虑如何处理它。

谢谢,但这与删除return false具有相同的效果。单击链接时,页面将跳过。