jQuery选项卡-获取先前选择的选项卡的索引 [英] jQuery tabs - Get the index of previously selected tab

查看:46
本文介绍了jQuery选项卡-获取先前选择的选项卡的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jQuery 1.7.1-我想获取先前选择的选项卡的索引.例如:如果我从1st移到3rd标签,我想将之前选择的标签索引设为0.如何实现?

jQuery 1.7.1 - I would like to get the index of previously selected tab. 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?

我尝试了,但这没有用.

I tried this, but that didn't work.

我有以下标记,

<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;
}});

推荐答案

在选择或显示回调触发时,您只能使用ui.index来获取当前选定的标签.最好的选择是仅跟踪该索引并在选项卡切换时对其进行更新,这会在更新之前告诉您先前的索引.

By the time the select or show callbacks fire you can only get the currently selected tag, by using 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;
    } 
});

这篇关于jQuery选项卡-获取先前选择的选项卡的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆