【问题标题】:Browser-like tabs - Jquery UI Tab positioning类似浏览器的选项卡 - Jquery UI 选项卡定位
【发布时间】:2011-09-22 07:51:32
【问题描述】:

我正在开发 JQuery UI 选项卡添加/删除功能。

我的html是

<div id="dialog" title="Tab data">
    <form>
        <fieldset class="ui-helper-reset">
            <label for="tab_title">Title</label>

            <input type="text" name="tab_title" id="tab_title" value="" class="ui-widget-content ui-corner-all" />
            <label for="tab_content">Content</label>
            <textarea name="tab_content" id="tab_content" class="ui-widget-content ui-corner-all"></textarea>
        </fieldset>
    </form>
</div>

<button id="add_tab">Add Tab</button>

<div id="tabs">
    <ul>

    </ul>

</div>

我的 JQuery 是

$(function() {
        var $tab_title_input = $( "#tab_title"),
            $tab_content_input = $( "#tab_content" );
        var tab_counter = 2;

        // tabs init with a custom tab template and an "add" callback filling in the content
        var $tabs = $( "#tabs").tabs({
            tabTemplate: "<li><a href='#{href}'>#{label}</a> <span class='ui-icon ui-icon-close'>Remove Tab</span></li>",
            add: function( event, ui ) {
                var tab_content = $tab_content_input.val() || "Tab " + tab_counter + " content.";
                $( ui.panel ).append( "<p>" + tab_content + "</p>" );
            }
        });

        // modal dialog init: custom buttons and a "close" callback reseting the form inside
        var $dialog = $( "#dialog" ).dialog({
            autoOpen: true,
            modal: true,
            buttons: {
                Add: function() {
                    addTab();
                    $( this ).dialog( "close" );
                },
                Cancel: function() {
                    $( this ).dialog( "close" );
                }
            },
            open: function() {
                $tab_title_input.focus();
            },
            close: function() {
                $form[ 0 ].reset();
            }
        });

        // addTab form: calls addTab function on submit and closes the dialog
        var $form = $( "form", $dialog ).submit(function() {
            addTab();
            $dialog.dialog( "close" );
            return false;
        });

        // actual addTab function: adds new tab using the title input from the form above
        function addTab() {
            var tab_title = $tab_title_input.val() || "Tab " + tab_counter;
            $tabs.tabs( "add", "#tabs-" + tab_counter, tab_title );
            tab_counter++;
        }

        // addTab button: just opens the dialog
        $( "#add_tab" )
            .button()
            .click(function() {
                $dialog.dialog( "open" );
            });

        // close icon: removing the tab on click

        $( "#tabs span.ui-icon-close" ).live( "click", function() {
            var index = $( "li", $tabs ).index( $( this ).parent() );
            $tabs.tabs( "remove", index );
        });
    });

我想要的是自动切换到新创建的标签并在标签旁边添加“新标签”按钮,就像我们这些天在浏览器中一样。

我尝试在标签&lt;ul&gt; 内插入“添加标签”按钮,但随后标签无法正常工作。

任何帮助将不胜感激!

更新

这是可行的解决方案:http://jsfiddle.net/SjW4f/20/

【问题讨论】:

    标签: jquery jquery-ui jquery-tabs


    【解决方案1】:

    这里是:http://jsfiddle.net/SjW4f/21/

    您需要对add 事件上的标签重新排序,以使add 成为最新标签。

    更新到工作版本

    【讨论】:

    • 嘿 Samich,感谢您的回答,但新创建的选项卡未显示内容...我的意思是第一个创建的选项卡显示内容但 2,3,4.... 无法显示内容..
    • 我猜'+'周围的锚标签有问题如果你删除锚标签,内容加载完美..
    • 是的!感谢您的时间和努力!我结合了你和阿利斯泰尔的答案来实现我想要的。这里是jsfiddle.net/SjW4f/20
    • 嘿,出于搜索原因,我已将问题的标题更新为更易于理解。
    【解决方案2】:

    http://jsfiddle.net/SjW4f/4/ 我改变了两件事。添加选项卡后,我添加了一个“选择”方法。我还将您的现场活动更改为委托活动。

    【讨论】: