我知道我可以得到當前選定的標籤的索引,但我可以以某種方式獲得ID(相當於ui.panel.id
,如果這是由標籤事件觸發的......但它是不是)當前選定的標籤?我不想使用索引,因爲選項卡的順序可能會改變。我更喜歡不使用樣式標記,因爲這些標記在未來的版本中可能會更改。有沒有一種方法呢?如果沒有,我可以以某種方式使用索引來訪問它(甚至可以通過首先訪問面板對象)?任何其他想法?需要當前選定的標籤ID爲jQuery標籤
回答
可以使用:visible
pseudo-selector目標的可視面板:
$("#tabs .ui-tabs-panel:visible").attr("id");
值得一提的是,你可以檢索從activate event活動標籤:
$("#tabs").tabs({
activate: function (event, ui) {
console.log(ui.newPanel[0].id);
}
});
正如我貼在回答this question,有幾種方法可以實現這一點。
在jQuery documents,他們建議做到以下幾點發現當前打開的選項卡的索引:
var $tabs = $('#example').tabs();
var selected = $tabs.tabs('option', 'selected'); // => 0
然而,這是不切實際的,如果你需要做大量的與該選項卡。 爲什麼他們還沒有提供獲取實際元素的更實用的解決方案,但我不確定,但是,通過使用jQuery,您可以創建一個簡單的解決方案。
在下面的代碼中,我會告訴你它是多麼容易的事要與當前標籤任何信息:id
(或實際上是href
)從所選的選項卡。如果您想
var curTab = $('.ui-tabs-panel:not(.ui-tabs-hide)'),
curTabIndex = curTab.index(), // will get you the index number of where it sits
curTabID = curTab.prop("id"), // will give you the id of the tab open if existant
curTabCls = curTab.attr("class"); // will give you an array of classes on this tab
// etc ....
// now, if you wanted a little more depth, for instance specific tabs area (if you have multiple tabs on your page) you can do simply add to your selector statement
var curTab = $('#myTabs_1 .ui-tabs-panel:not(.ui-tabs-hide)');
// then you can make simple calls to that tab and get whatever data or manipulate it how you please
curTab.css("background-color", "#FFF");
,您可以使用eq()
來檢索jQuery對象。
這裏你可以看到一個例子:http://jsfiddle.net/svierkant/hpU3T/1/
var curTab = $jQuery('#tabs .ui-tabs-panel:not(.ui-tabs-hide)').attr('id');
選擇已被棄用
的JQuery 1.9之後所以使用
var active = $("#jtabs").tabs("option", "active");
喬納森·桑普森的答案不工作了。試試...
$("#tabs .ui-tabs-panel:visible").attr("id");
的jsfiddle:http://jsfiddle.net/tbEq6/
$("#tabs .ui-state-active a").attr("id");
這工作:
$('#divName .ui-tabs-panel[aria-hidden="false"]').prop('id');
請給你的答案添加一些解釋。 – 2015-03-11 09:14:55
對於jQuery UI的> = 1.9,你可以使用ui.newPanel.selector
:
$('#tabs').on('tabactivate', function(event, ui) {
console.log(ui.newPanel.selector);
});
對於jquery版本低於1。9:
<div class="roundedFloatmenu">
<ul id="unid">
<li class="titleHover" id="li_search">Search</li>
<li class="titleHover" id="li_notes">Notes</li>
<li class="titleHover active" id="li_writeback">Writeback</li>
<li class="titleHover" id="li_attorney">Attorney</li>
</ul>
</div
,你可以找到使用有源標籤:
jQuery('#unid').find('li.active').attr('id')
- 1. 需要定製的jQuery標籤的CSS
- 2. 如何檢測加載標籤ID或我想要當前加載標籤ID(這是不是當前選定的標籤)使用chrome.tab方法
- 3. 無標籤「選擇」標籤庫中定義的前綴爲「c」
- 4. jQuery的標籤 - 不要通過#加載標籤(標籤內容ID)
- 5. 設置活動標籤當前標籤
- 6. JQuery選擇特定標籤
- 7. 當在css或jquery中點擊標籤時,需要突出顯示標籤
- 8. 變化的jQuery當前標籤
- 9. 給予ID jQuery的標籤
- 10. jQuery CSS - 不要選擇<img>標籤<a>標籤
- 11. jQuery選項卡 - 當前選項卡中的參考標籤?
- 12. Jquery通過ID選擇標籤
- 13. JQuery Simplemodal和標籤幫助需要
- 14. jQuery UI標籤幫助需要請
- 15. jquery選中標籤
- 16. 如何獲得我在Jquery標籤中命名的當前標籤標題UI
- 17. 獲取Facebook頁面標籤當前標籤的標識?
- 18. 如何使用星號標記當前標籤的標籤?
- 19. jQuery標籤 - 基於標籤文本選擇標籤
- 20. jQuery的語法錯誤,當標籤ID包含`$`簽署
- 21. 通過標籤ID或名稱爲特定標籤創建single.php
- 22. 爲什麼H1標籤不需要在CSS中的類或ID?
- 23. iOS:選中標籤前的標籤欄項目標題
- 24. 需要修復android標籤
- 25. 檢測當前標籤
- 26. 活動/當前標籤css
- 27. 添加標籤當前BeautifulSoup
- 28. 關閉當前標籤
- 29. 需要幫助鏈接標籤,但我想要多個標籤
- 30. 標籤系統中標籤的ID
問題的變化將是:是否有一種方式來獲得從索引標籤面板? – 2009-12-08 23:38:55
[jQuery ui選項卡的可能重複:獲取被激活的選項卡(div)的id](http://stackoverflow.com/questions/1864219/need-currently-selected-tab-id-for-jquery-tabs) – mins 2015-03-11 06:08:18