jQuery选项卡 - 获取新选择的索引

26

我之前使用过jquery-ui tabs扩展通过ajax加载页面片段,并隐藏或显示页面内部的隐藏div。这两种方法都有很好的文档,我没有遇到任何问题。

然而,现在我想用选项卡做一些不同的事情。当用户选择一个选项卡时,应该重新加载整个页面 - 这样做的原因是每个选项卡部分的内容在渲染时有点昂贵,所以我不想一次性发送它们并使用切换"display:none"来显示它们。

我的计划是拦截选项卡的select事件,并通过操作document.location函数重新加载页面。

select处理程序中,如何获取新选择的选项卡索引和相应的html LI对象?

$('#edit_tabs').tabs(  {
        selected: 2,     // which tab to start on when page loads
        select: function(e, ui) {
            var t = $(e.target);
            // alert("data is " +  t.data('load.tabs'));  // undef
            // alert("data is " +  ui.data('load.tabs'));  // undef

            // This gives a numeric index...
            alert( "selected is " + t.data('selected.tabs') )
            // ... but it's the index of the PREVIOUSLY selected tab, not the
            // one the user is now choosing.  
            return true;

            // eventual goal is: 
            // ... document.location= extract-url-from(something); return false;
        }
});

有没有事件或ui对象的属性可以读取,以获取新选择的选项卡的索引、id或对象,或者其中的锚标签?

还是有更好的方法使用选项卡重新加载整个页面?

8个回答

37

我会查看Tabs的事件。以下内容摘自jQuery文档:

 $('.ui-tabs-nav').bind('tabsselect', function(event, ui) {
     ui.options // options used to intialize this widget
     ui.tab // anchor element of the selected (clicked) tab
     ui.panel // element, that contains the contents of the selected (clicked) tab
     ui.index // zero-based index of the selected (clicked) tab
 });

看起来使用ui.tab是最好的选择。


7

在jQuery UI - v1.9.2中

ui.newTab.index()

获取当前活动标签页的基础0索引


5

5
@Ghommey: 距离那时已经近2年了,再次看到这个答案对我仍然很有帮助。 - Michael Borgwardt
@MichaelBorgwardt 好酷啊 :) 不过 Klaus 和 Matt 两年前就给出了类似的答案。 - jantimon

0

0

或者我在我的网站上使用的另一个选项是这个。它是一个基本的UL/Div标签导航系统。点击带有哈希标记的链接到另一页时,正确触发选项卡的关键是通过过滤您的UL来查找与浏览器传递的#example相匹配的内容。就像这样。

这是示例HTML:

    <div id="tabNav">

  <ul class="tabs">
    <li><a href="#message">Send a message</a></li>
    <li><a href="#shareFile">Share a file</a></li>
    <li><a href="#arrange">Arrange a meetup</a></li>
  </ul>
</div>

<div id="tabCont">

  <div id="message">
    <p>Lorem ipsum dolor sit amet.</p>
  </div>
  <div id="shareFile">
    <p>Sed do eiusmod tempor incididunt.</p>
  </div>
  <div id="arrange">
    <p>Ut enim ad minim veniam</p>
  </div>

</div>

实现它的Jquery代码:

$(document).ready(function() {
$(function () {
    var tabs = [];
    var tabContainers = [];

    $('ul.tabs a').each(function () {
      // note that this only compares the pathname, not the entire url
      // which actually may be required for a more terse solution.
        if (this.pathname == window.location.pathname) {
            tabs.push(this);
            tabContainers.push($(this.hash).get(0));
        }
    });

    $(tabs).click(function () {
        // hide all tabs
        $(tabContainers).hide().filter(this.hash).show();


        // set up the selected class
        $(tabs).removeClass('active');
        $(this).addClass('active');

        return false;

});




 $(tabs).filter(window.location.hash ? '[hash=' + window.location.hash + ']' : ':first').click();

  });

});

这应该能为您解决问题。我知道这不是最干净的代码,但它可以帮助您实现目标。


0

谢谢,jobscry - 你所指出的 'ui.tab' 给了我点击的锚点标签,从中我可以提取它的 class、id、href 等等... 我选择使用 id 编码我的 url。我的最终 tabs() 调用如下:

$(document).ready(function() {
    $('#edit_tabs').tabs(  {
        selected: [% page.selected_tab ? page.selected_tab : 0 %],
        select: function(e, ui) {
            // ui.tab is an 'a' object
            // it has an id of "link_foo_bar"
            // transform it into http://....something&cmd=foo-bar
            var url = idToTabURL(ui.tab.id);

            document.location = url;
            return false;
        }
    }).show();
});

0

在我的实现中,它的工作方式如下:

$(document).ready(function() {
    $('#edit_tabs').tabs(  {
        selected: [% page.selected_tab ? page.selected_tab : 0 %],
        select: function(e, ui) {
            // ui.tab is an 'a' object
            var url = ui.tab.href;

            document.location = url;
            return false;
        }
    }).show();
});

0
快速查看文档,我们就能找到解决方案:
$('#edit_tabs').tabs({ selected: 2 }); 

上述语句将激活第三个选项卡。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接