简体   繁体   English

JQuery 选项卡,无法更改激活选项卡

[英]JQuery Tabs , can´t change activate tab

i'm new here and didn´t find the solution for my problem.我是新来的,没有找到解决我问题的方法。 i searched the web and tried tons of solutions but they didn´t work.我在网上搜索并尝试了大量解决方案,但都没有奏效。

i use Jquery Tabs with the closable feature( http://jsfiddle.net/42er3/9/ )我使用具有可关闭功能的 Jquery Tabs( http://jsfiddle.net/42er3/9/

Now i wanted to activate the latest created Tab.现在我想激活最新创建的选项卡。 This doesn´t work ^^这不起作用^^

Here is my Code:这是我的代码:

$(function () {
    var tabTitle = $("#tab_title"),
        tabContent = $("#tab_content"),
        tabTemplate = "<li><a href='#{href}'>#{label}</a> <span class='ui-icon ui-icon-close' role='presentation'>Remove Tab</span></li>",
        tabCounter = 1;
    var tabs = $("#tabs").tabs();

    // modal dialog init: custom buttons and a "close" callback resetting the form inside
    var dialog = $("#dialog").dialog({
        autoOpen: false,
        modal: true,
        buttons: {
            Send: function () {
                addTab();
                $(this).dialog("close");
            },
            Cancel: function () {
                $(this).dialog("close");
            }
        },
        close: function () {
            form[0].reset();
        }
    });
    // addTab form: calls addTab function on submit and closes the dialog
    var form = dialog.find("form").submit(function (event) {
        addTab();
        dialog.dialog("close");
        event.preventDefault();
    });

    // actual addTab function: adds new tab using the input from the form above
    function addTab() {
        var label = tabTitle.val() || "Tab " + tabCounter,
            id = tabTitle.val(),
            li = $(tabTemplate.replace(/#\{href\}/g, "#" + id).replace(/#\{label\}/g, label)),
            tabContentHtml = tabContent.val() || "Tab " + tabCounter + " content.";

        tabs.find(".ui-tabs-nav").append(li);

        tabs.append("<div id='" + id + "' class='rel'><p>" + tabContentHtml + "</p><hr /><div id='writebox'><form name='send' id ='sent'><textarea name ='msg' class='boxsizingBorder'></textarea><input type='submit' name='" + id + "' /></form></div></div>");

        //after added the tab, active it
        tabs.tabs({
            active: tabCounter
        }); // <---------------------------Here it is

        //alert(panelId+"-"+tabCounter);
        tabCounter++;
        tabs.tabs("refresh");

    }
    // addTab button: just opens the dialog
    $("#add_tab")
        .button()
        .click(function () {
            dialog.dialog("open");
        });
    // close icon: removing the tab on click
    tabs.delegate("span.ui-icon-close", "click", function () {
        var panelId = $(this).closest("li").remove().attr("aria-controls");
        $("#" + panelId).remove();
        tabs.tabs("refresh");
    });
    tabs.bind("keyup", function (event) {
        if (event.altKey && event.keyCode === $.ui.keyCode.BACKSPACE) {
            var panelId = tabs.find(".ui-tabs-active").remove().attr("aria-controls");
            $("#" + panelId).remove();
            tabs.tabs("refresh");
        }
    });
});

You can use the following code to programatically make a tab active您可以使用以下代码以编程方式使选项卡处于活动状态

$( "#tabs" ).tabs( "option", "active", 2 );

I've updated the addTab() method in your Fiddle as follows:我已经更新了你的 Fiddle 中的 addTab() 方法如下:

function addTab() {
    ...
    tabs.tabs("refresh");
    tabs.tabs( "option", "active", tabCounter-1 );
    tabCounter++;
}

As tab indexes start at 0 rather than 1, I've had to subtract 1 from tabCounter to get the index of the tab that was just added.由于选项卡索引从 0 而不是 1 开始,我必须从tabCounter减去 1 才能获得刚刚添加的选项卡的索引。

See here for a demo在这里查看演示

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

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