jquery 添加页面加载显示请求的选项卡功能
我是 jquery 的新手,我已使用此代码作为选项卡
<script type="text/javascript" charset="utf-8">
$(function () {
var tabContainers = $('div.tabs > div');
tabContainers.hide().filter(':first').show();
$('div.tabs ul.tabNavigation a').click(function () {
tabContainers.hide();
tabContainers.filter(this.hash).show();
$('div.tabs ul.tabNavigation a').removeClass('selected');
$(this).addClass('selected');
return false;
}).filter(':first').click();
});
</script>
我需要的是在打开如下网址时能够加载特定选项卡: www.mysite.com/page.html#tab1
html 链接如下所示:
<a href="#tab1" class="selected">name link</a>
<a href="#tab2" class="">name link2</a>
<a href="#tab3" class="">name link3</a>
我已经尝试了很多脚本,但也许是我的错(我是一个使用 javascript 的家伙)
提前谢谢您
I'm a newbie with jquery and i've used this code for tabs
<script type="text/javascript" charset="utf-8">
$(function () {
var tabContainers = $('div.tabs > div');
tabContainers.hide().filter(':first').show();
$('div.tabs ul.tabNavigation a').click(function () {
tabContainers.hide();
tabContainers.filter(this.hash).show();
$('div.tabs ul.tabNavigation a').removeClass('selected');
$(this).addClass('selected');
return false;
}).filter(':first').click();
});
</script>
What i need is to be able to load a specific tab when open a url like:
www.mysite.com/page.html#tab1
html link looks like this:
<a href="#tab1" class="selected">name link</a>
<a href="#tab2" class="">name link2</a>
<a href="#tab3" class="">name link3</a>
I've tryied lots of scripts, but maybe is my fault (i'm a dude with javascript)
Thank you in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
JQuery-UI 有一个 tab-widget 可以使用。
比自己编写更简单、更稳定。
对于不同的 URL,请查看这篇文章。
它告诉您如何利用 UI 主题并加载不同的 URl。
否则,您必须对选项卡中的点击事件执行
$('#theIdOfTheDiv').load(url)
JQuery-UI have a tab-widget You can use.
Much simpler and more stable than writing your own.
For different URL's please have a look at this post.
It tells you how to leverage on the UI-themes and load different URl's.
Otherwise You have to do a
$('#theIdOfTheDiv').load(url)
on the click event in the tab您需要一个像
点击查看tab1
您需要将用户重定向到 #tab1,因此必须删除您的
return false;
行禁用重定向的函数您的函数正在被触发当点击
时,需要在页面加载时再次触发
您的功能将如下所示
You need an anchor like
<a href="#tab1">click to see tab1</a>
You need to redirect the user to #tab1 so you have to remove the
return false;
line of your function wich disables the redirectingYour function is being triggered when the
<a>
is clicked, you need to trigger it once more when the page is being loadedyour function will look like this
您希望默认显示的选项卡在 HTML 中应具有
selected
类。相关的内容包装器也应该如此。其他人不应该。The tab you want displayed by default should have a class of
selected
in your HTML. So should the associated content wrapper. The others should not.window.location 有一个 hash 属性。因此,您可以对照空字符串检查它,如果它不为空,请选择该选项卡:
window.location has a hash attribute. So, you can check it against the empty string and if it's not empty, select that tab: