JavaScript 选项卡式界面 - 如何链接选项卡并为选项卡添加书签?
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以设置
window.location.hash
以及读取它:请注意,这会将页面添加到浏览器历史记录中,允许用户使用后退和前进按钮来更改哈希值。理想情况下,您还应该考虑处理这个问题。
You can set
window.location.hash
as well as reading it: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.