可访问的 jquery 选项卡 - 直接链接到选项卡
我的页面上有选项卡。它们的代码如下:
$('ul#tabs li a').click(function(){
var id = $(this).attr('id');
var counter = 1;
$("ul#tabs a.current").removeClass('current');
$(this).addClass('current');
$('.contentBox').not('.' + id).hide();
$('.contentBox.' + id).show();
if(id=='all'){
$('.contentBox').show();
}
$('.contentBox').removeClass('last');
$('.contentBox').each(function() {
if(counter%4==0) {
$(this).addClass('last');
}
if($(this).css('display')!='none'){
counter++;
}
});
return false;
});
现在我想添加直接链接的可能性,以便用户可以将链接复制到某个选项卡并将其粘贴到地址栏,然后立即转到该选项卡/div。像这样:
TabLinkforContent1 TabLinkforContent2 TabLinkforContent3
Atm 在页面加载时显示 TabLinkforContent1
,现在用户需要单击 TabLinkforContent3
链接来查看 div3
上的内容。我想更改代码,以便用户可以复制 TabLinkforContent3
地址并将其提供给某人,如果他使用该网址,它会自动显示内容 div3
。所以我猜 url 应该是这样的 www.domain.com/page#div3
。但我不知道如何更改 javascript 代码。
任何帮助将不胜感激:)
I have tabs on my page. The code for them is following:
$('ul#tabs li a').click(function(){
var id = $(this).attr('id');
var counter = 1;
$("ul#tabs a.current").removeClass('current');
$(this).addClass('current');
$('.contentBox').not('.' + id).hide();
$('.contentBox.' + id).show();
if(id=='all'){
$('.contentBox').show();
}
$('.contentBox').removeClass('last');
$('.contentBox').each(function() {
if(counter%4==0) {
$(this).addClass('last');
}
if($(this).css('display')!='none'){
counter++;
}
});
return false;
});
Now I would like to add possibility to direct linking so that user can copy the link to certain tab and paste it to address bar and it goes to that tab/div straight away. Like this:
TabLinkforContent1 TabLinkforContent2 TabLinkforContent3
Atm when page is loaded it shows the TabLinkforContent1
and now user needs to click for example the TabLinkforContent3
link to vie Content on div3
. I want to change the code so that use can copy TabLinkforContent3
address and give it to someone and if he uses that url it shows automatically content div3
. So url would be something like www.domain.com/page#div3
i guess. But I dont know how to change the javascript code.
Any help would be appreciated :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您的链接是基于 ID 的,因此您可以将其放在当前代码之后:
这将触发您刚刚在
click
处理程序code> 元素具有标识。结合您当前的代码缩短:Since your links are ID based, you can just put this after your current code:
This will trigger the
click
handler you just bound on the<a id="something">
element the has identifies. Combined with your current code shortened down: