简体   繁体   English

jQuery选项卡 - 获取以前选择的选项卡的索引

[英]jQuery tabs - Get the index of previously selected tab

jQuery 1.7.1 - I would like to get the index of previously selected tab. jQuery 1.7.1 - 我想获得以前选择的选项卡的索引。 Ex: If I move to the 3rd tab from 1st, i would like to get the previous selected tab index as 0. How to achieve this? 例如:如果我从1st移动到第3个选项卡,我想将之前选择的选项卡索引设为0.如何实现此目的?

I tried this , but that didn't work. 我试过这个 ,但那没用。

I have the following markup, 我有以下标记,

<div id="tabs">
   <ul>
      <li><a href="t1" title="content">Gallery</a></li>
      <li><a href="t2" title="content">Polls</a></li>
      <li><a href="t3" title="content">Events</a></li>
   </ul>
   <div id="content"></div>
</div>

Javascript, JAVASCRIPT,

$('#tabs').tabs(  {
    select: function(e, ui) {
    var t = $(e.target);
    alert( "Index " + t.data('selected.tabs') );
    return true;
}});

By the time the select or show callbacks fire you can only get the currently selected tag, by using ui.index . 当select或show回调触发时,您只能使用ui.index获取当前选择的标记。 Your best bet is to just track that index and update it upon tab switching, which will tell you the previous index before said updating. 您最好的选择是跟踪该索引并在选项卡切换时更新它,这将在更新之前告诉您之前的索引。

var previousIndex = 0;
$('#tabs').tabs(  {
    select: function(e, ui) {
        //do whatever you need to do with previousIndex
        alert("The previously selected tab index was " + previousIndex);
        //track the new index
        previousIndex = ui.index;
    } 
});

you can use $(this).tabs('option', 'selected') in select: option 你可以在select:选项中使用$(this).tabs('option', 'selected')

like 喜欢

$("#tabs").tabs({
      select: function(event, ui){
          console.log($(this).tabs('option', 'selected')}
      )}
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM