简体   繁体   English

JQuery UI 1.11将选项卡设置为活动状态

[英]JQuery UI 1.11 Set tab to active

I have Jquery 1.11 and I have been trying for hours to get the newly created tab to be set to active and show it. 我有Jquery 1.11,我已经尝试了几个小时来将新创建的选项卡设置为活动并显示它。

Jsfiddle 的jsfiddle

I also would like to add an "x" on the tab to close the tab. 我还想在选项卡上添加“x”以关闭选项卡。

I have went through a lot of posts on here and they are mainly for older deprecated methods. 我在这里经历了很多帖子,它们主要是针对较旧的弃用方法。

Javascript: 使用Javascript:

$(function() {
  var tabs = $( "#search-tabs" ).tabs({
        heightStyle: "fill"
    });
    tabs.find( ".ui-tabs-nav" ).sortable({
        axis: "x",
        stop: function() {
            tabs.tabs("refresh");
        }
    });


    $('button#addtab').click(function(){
        var num_tabs = $("div#search-tabs ul li").length + 1;

        $("div#search-tabs ul").prepend(
            "<li><a href='#s-tab" + num_tabs + "'>#" + num_tabs + "</a></li>"
        );

        $("div#search-tabs").append(
            "<div id='s-tab" + num_tabs + "'>#" + num_tabs + "</div>"
        );

        $("#search-tabs").tabs("refresh");
});

});

View: 视图:

  <button id="addtab">Add Tab</button>
  <div id="search-tabs" >
    <ul>
    </ul>
  </div>

You've got the first half of what you want: calling .tabs("refresh") causes the new tab to be picked up and added to the list. 你得到了你想要的前半部分:调用.tabs("refresh")会导致新选项卡被选中并添加到列表中。 All that you're missing is setting the active tab after you've refreshed the tabs. 在您刷新选项卡后,您所缺少的只是设置活动选项卡。 Looking at the API documentation for the jQuery UI Tabs, we'll see that there's an option in the Tabs API called "active". 查看jQuery UI选项卡的API文档 ,我们将看到Tabs API中有一个名为“active”的选项。

active 活性

Type: Boolean or Integer 类型:布尔值或整数

Default: 0 默认值:0

Which panel is currently open. 哪个面板目前是开放的。 Multiple types supported: 支持多种类型:

  • Boolean: Setting active to false will collapse all panels. Boolean:将active设置为false将折叠所有面板。 This requires the collapsible option to be true. 这要求可折叠选项为真。

  • Integer: The zero-based index of the panel that is active (open). 整数:活动(打开)的面板的从零开始的索引。 A negative value selects panels going backward from the last panel. 负值选择从最后一个面板向后移动的面板。

This is going to be the option we need to change. 这将是我们需要改变的选择。 Looking at the methods available, we'll see that this can be done using the option( optionName, value ) method, like this: 查看可用的方法,我们将看到这可以使用option( optionName, value )方法完成,如下所示:

$("#search-tabs").tabs("refresh");
$("#search-tabs").tabs( "option", "active", 0 );

Since your new tabs are being added to the first position, you can just set the active option to zero and that'll point to the newly added tab! 由于您的新标签被添加到第一个位置,您只需将活动选项设置为零,并指向新添加的标签!

Here's a demo on jsFiddle: http://jsfiddle.net/pb39g4b3/ . 这是关于jsFiddle的演示: http//jsfiddle.net/pb39g4b3/

As @sphanley has suggested. 正如@sphanley建议的那样。 Using $("#search-tabs").tabs( "option", "active", 0 ); 使用$("#search-tabs").tabs( "option", "active", 0 ); is the correct way of doing it. 这是正确的做法。

Further, you can implement the close functionality as shown here . 此外,您可以实现此处所示的关闭功能。

Here is the demo link with all features you requested. 以下是包含您请求的所有功能的演示链接。 http://jsfiddle.net/eeLrgxxt/7/ http://jsfiddle.net/eeLrgxxt/7/

you can do it this way 你可以这样做

http://jsfiddle.net/dm96hvb9/

you need to append the class="ui-tabs-active ui-state-active" and then show the matching div 你需要附加class =“ui-tabs-active ui-state-active”然后显示匹配的div

$('button#addtab').click(function(){
    var num_tabs = $("div#search-tabs ul li").length + 1;

    // remove any active tab
    $('#search-tabs li').removeClass('ui-state-active');
    $('#search-tabs li').removeClass('ui-tabs-active');

    // added the active class to the li
    $("div#search-tabs ul").prepend(
        "<li class='ui-tabs-active ui-state-active'><a href='#s-tab" + num_tabs + "'>#" + num_tabs + "</a></li>"
    );

    $("div#search-tabs").append(
        "<div id='s-tab" + num_tabs + "'>#" + num_tabs + "</div>"
    );

    $("#search-tabs").tabs("refresh");

    //show the active div
    $("#s-tab"+num_tabs).show();

}); });

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

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