JavaScript 选项卡式界面 - 如何链接选项卡并为选项卡添加书签?

发布于 2024-10-20 11:04:25 字数 1446 浏览 1 评论 0原文

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

我希望能够链接到特定选项卡并允许用户为他们所在的当前选项卡添加书签。

当我为每个包含的 div 使用 ID 时,我可以通过从点击事件中删除 return false; 来实现此目的,但这会导致页面跳到选项卡的包含 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>

非常感谢任何帮助。

I’ve created a tabbed interface with jQuery to show/hide content.

I want to be able to link to a particular tab and allow users to bookmark the current tab they are on.

As I use IDs for each containing div I can achieve this by removing return false; from the click event but this causes the page to skip down to the containing div for the tab.

Is there a way of ensuring that the URL contains the # part of the address but prevents the page from skipping? Is there another way of approaching this problem that I haven’t come across yet?

//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>

Any help is much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

够运 2024-10-27 11:04:25

您可以设置window.location.hash以及读取它:

$("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;

});

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

You can set window.location.hash as well as reading it:

$("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;

});

Note that this will add a page into the browser history, allowing the user to use the back and forward buttons to change the hash. You should ideally also look at handling that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文