jQuery 选项卡:如何创建指向特定选项卡的链接?
我正在为选项卡使用一个简单的 jQuery 脚本:
JS:
$(document).ready(function() {
$(".tab-content").hide();
$("ul.tabs li:first").addClass("active").show();
$(".tab-content:first").show();
$("ul.tabs li").click(function() {
$("ul.tabs li").removeClass("active");
$(this).addClass("active");
$(".tab-content").hide();
var activeTab = $(this).find("a").attr("href");
$(activeTab).show();
return false;
});
});
HTML(对于index.html):
<div id="tabs">
<ul class="tabs">
<li><a href="#tabs-voters">Top Voters</a></li>
<li><a href="#tabs-commenters">Top Commenters</a></li>
</ul>
<div id="tabs-voters" class="tab-content">
<p id="h1-01">Tab content</p>
<p>Some content</p>
</div>
<div id="tabs-commenters" class="tab-content">
<h2 id="h-02">Tab content</h2>
<p>Some content</p>
<h2 id="h-03">Tab content</h2>
<p>Some content</p>
</div>
</div>
我需要做的是创建一个到 index.html#h-02 的工作链接、index.html#h-03 等,但这些简单的链接不起作用,因为该选项卡默认是隐藏的。是否可以修改JS,以便我可以链接到打开index.html时隐藏的选项卡中的书签?有人能指出我正确的方向吗?
多谢! :)
I'm using a simple jQuery script for tabs:
The JS:
$(document).ready(function() {
$(".tab-content").hide();
$("ul.tabs li:first").addClass("active").show();
$(".tab-content:first").show();
$("ul.tabs li").click(function() {
$("ul.tabs li").removeClass("active");
$(this).addClass("active");
$(".tab-content").hide();
var activeTab = $(this).find("a").attr("href");
$(activeTab).show();
return false;
});
});
The HTML (for the index.html):
<div id="tabs">
<ul class="tabs">
<li><a href="#tabs-voters">Top Voters</a></li>
<li><a href="#tabs-commenters">Top Commenters</a></li>
</ul>
<div id="tabs-voters" class="tab-content">
<p id="h1-01">Tab content</p>
<p>Some content</p>
</div>
<div id="tabs-commenters" class="tab-content">
<h2 id="h-02">Tab content</h2>
<p>Some content</p>
<h2 id="h-03">Tab content</h2>
<p>Some content</p>
</div>
</div>
What I need to do is to create a working link to index.html#h-02, index.html#h-03 etc., but these simple links don't work because the tab is hidden by default. Is it possible to modify the JS, so I can link to a bookmark in tabs that are hidden when opening index.html? Can someone point me in the right direction?
Thanks a lot! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在文档就绪处理程序中,您可以检查片段的值并使用 JavaScript 显示相应的选项卡。
按要求编辑:
编辑2:
如果您希望能够指定选项卡内容元素的ID(例如
h-02
)您链接的页面),然后您必须向后获取相应选项卡的 ID 才能激活它。像这样:使用
$.closest()
意味着哈希可以指定.tab-content
本身(例如示例中的tabs-commenters
)或其子项的 ID。我在上面也提出了一些其他清理建议。无需不断重新选择那些 DOM 元素!
In your document ready handler, you can examine the value of the fragment and use JavaScript to show the corresponding tab.
Edit as requested:
Edit 2:
If you want to be able to specify an ID of a tab content element (like
h-02
in the page you linked) then you have to work backwards to get the ID of the corresponding tab to activate it. Like this:Using
$.closest()
means that the hash can specify the ID of the.tab-content
itself (such astabs-commenters
in your example), or a child thereof.I've made some other cleanup suggestions as well above. No need to keep re-selecting those DOM elements!
似乎没有什么对我有用:
nothing seems to work for me:
那么,您可以将 URL 设置为类似于
http://site.com/index.html?voters
的形式,然后在页面中检查window.location.search
(搜索是包括问号的位)。如果它是"?voters"
那么您运行代码来选择该选项卡。如果它是"?commenters"
那么您运行代码来选择该选项卡。等等。Well, you can make your URL something like
http://site.com/index.html?voters
and then in the page, you checkwindow.location.search
(search being the bit including the question mark). If it's"?voters"
then you run the code to select the tab. If it's"?commenters"
then you run the code to select that tab. Et cetera.我会尝试稍微修改一下你的代码。这就是我要做的:
http://jsfiddle.net/RtCnU/8/
这让您准确地完成您想要的事情。
这是我使用的代码:
这是 Javascript:
让我知道它是如何工作的!
I would try to modify your code a bit. This is how I would do it:
http://jsfiddle.net/RtCnU/8/
This way you accomplish exactly what you wanted.
This is the code that I used:
and this is the Javascript:
Let me know how it works out!
您可以通过 GET 将参数传递到网页。
即
www.yourpage.com?tab=tab1
然后例如(使用 php)
然后在您的 JS 代码中,您可以执行以下操作:
尽管您应该检查以确保 default_id 有效,并且如果不加载工作默认值(就像您已经做的那样)
编辑我刚刚注意到问题是想要使用格式类似于 www.example.com#h-02 的 URL,其中您最好坚持使用只使用JS
You could pass a parameter to the webpage via GET.
i.e.
www.yourpage.com?tab=tab1
then for example (using php)
then in your JS code, you can do something like:
though you should check to make sure the default_id is valid, and if not load a working default(like you already do)
Edit I just noticed the question was wanting to use URLs formated like, www.example.com#h-02 in which you're better off sticking to using JS only
根据马特的回答,这就是我为让我的工作而所做的。我发布此内容是为了帮助其他人。
This is what I did to get mine to work, based on Matt's answer. I posted this to help others.
我喜欢丹·鲍尔斯的回答,我目前正在使用它的一个版本。不过,一开始我认为它不起作用,所以我发出了
alert(window.location.hash)
并发现它包含#
。 Dan 的答案是有效的,因为他在标签 ID 中使用了#
,例如tab="#tab_general"
。所以,要注意这一点。如果您想读取不带#的哈希名称,例如
tab="tab_general"
,请将:替换为
您当然也可以将
tab
更改为id 或其他东西。
我还没有弄清楚如何链接到嵌套选项卡,例如当一个选项卡包含带有子选项卡的页面时。
I like Dan Powers answer, I'm currently using a version of that. I did however, at first, think it wasn't working so I did a
alert(window.location.hash)
and found out that it included the#
. Dan's answer is valid though since he does use#
in his tab ids, e.g.tab="#tab_general"
. So, be aware of this.If you want to read your hash name without the #, e.g.
tab="tab_general"
, replace:with
You can of course also change
tab
toid
or something else.I haven't figured out how to link to nested tabs though, when e.g. one tab contains a page with sub tabs.