2013-07-16 99 views
0

我得到这个脚本:http://www.htmldrive.net/items/show/542/Simple-Tabs-w-CSS-jQuery.htmljQuery的选项卡:如何直接链接到一个标签

现在我想知道:是否有可能创建一个链接到一个标签?喜欢的东西exemple.com/#tab4

感谢, 马里亚诺

+0

看 - 你可以得到的哈希值:

你会得到这样的事情:

$(window).bind('hashchange', function() { hash = window.location.hash; if (hash) { elem = $('ul.tabs li:has(a[href="'+hash+'"])'); //Select the li targeted $("ul.tabs li").removeClass("active"); //Remove any "active" class elem.addClass("active"); //Add "active" class to selected tab $(".tab_content").hide(); //Hide all tab content var activeTab = elem.find("a").attr("href"); //Find the rel attribute value to identify the active tab + content $(activeTab).fadeIn(); //Fade in the active content return false; }; }); 

参考从那个网址,然后只是用javascript设置选项卡。谷歌可以为你做:) – Archer

回答

1

首先,你必须检测与哈希变化:

$(window).bind('hashchange', function() { 

    //your code here... 

}); 

然后用window.location.hash你会得到#hash,你可以用它来做和你代码中的click事件相同的事情。在`window.location.hash`

http://api.jquerymobile.com/hashchange/

http://api.jquery.com/bind/

+0

它的工作原理,如果我把它放在网址中,然后按enter ...但为什么它不起作用,如果我把它放在一个

? 此外,当点击标签时,URL不会改变 – Mark

+0

以在点击标签时更改URL,您可以在var activeTab = $(this)之后添加此'window.location.hash = activeTab;'。找到(“a”)。attr(“href”);'在'click()'事件 –

+0

好的,谢谢......为什么它没有在提交表单后在#tabN打开? 也刷新它总是去tab1即使在URL中有另一个选项卡指定 – Mark

相关问题