2013-07-16 29 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

相關問題