简体   繁体   English

通过单击jQuery UI选项卡上的+按钮来复制最后一个选项卡

[英]Duplicating last tab by clicking + button with jQuery UI Tabs

I need to create a tabbed page where the user can click a tab / button to duplicate the last tab as shown in the image below: 我需要创建一个选项卡式页面,用户可以在其中单击选项卡/按钮来复制最后一个选项卡,如下图所示:

点击加号添加标签

In this case, you'd click the plus sign and TAB 3 would be created with the same contents as TAB 2 . 在这种情况下,您将单击加号,将创建TAB 3 ,其内容与TAB 2相同。

I'm wondering what would be the best way to go about this? 我想知道最好的方法是什么?

Edit: 编辑:

Here's a JSFiddle (be sure to hit run first) with the code I've so far. 这是一个JSFiddle (一定要先运行),其中包含我到目前为止的代码。 It's almost there, but if your tab name has a space it doesn't dupe the last child html for some reason. 它几乎在那儿,但是如果您的标签名带有空格,则由于某种原因它不会重复最后一个子HTML。

It is better to manually generate id rather than directly using the user input to generate id , since it should be unique, it can not contain spaces etc. 最好手动生成id而不是直接使用用户输入来生成id ,因为它应该是唯一的,不能包含空格等。

We can set the tab's title to user input while generating unique id behind the scene. 我们可以将标签的标题设置为用户输入,同时在幕后生成唯一的id

I've updated your code by making use of jquery clone() method as shown below: 我已经通过使用jquery clone()方法更新了您的代码,如下所示:

 $(function() { var $tabs = $("#tabs").tabs(), $dialog = $("#new_tab_dialog").dialog({ autoOpen: false, height: 200, width: 350, modal: true, buttons: { "Create a new tab": function() { $dialog.dialog("close"); var tid = parseInt($(".tab").last().attr("id").replace("tab", "")) + 1, li = $("<li/>").insertBefore("#list li:last"); $("<a/>", { text: $("#new_tab_input").val(), href: "#tab" + tid }).appendTo(li); $("#tabs div.tab:last").clone().attr("id", "tab" + tid).appendTo("#tabs"); $tabs.tabs("refresh").tabs("option", "active", -2); alert(tid); }, Cancel: function() { $dialog.dialog("close"); } }, open: function() { $("#new_tab_input").val(""); } }); $("#create_tab").click(function() { $dialog.dialog("open"); }); }); 
 input.text { margin-bottom: 12px; width: 95%; padding: .4em; } 
 <link href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css" rel="stylesheet" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script> <div id="tabs"> <ul id="list"> <li><a href="#tab1">Tab 1</a> </li> <li><a href="#tab2">Tab 2</a> </li> <li><a href="#new_tab_dialog" id="create_tab">+</a> </li> </ul> <div id="tab1" class="tab"> <p>Tab 1 content.</p> </div> <div id="tab2" class="tab"> <p>Tab 2 content.</p> <p>When you click the plus it should create a new tab (and corresponding div) by duplicating the last child in the list.</p> </div> </div> <div id="new_tab_dialog" title="Duplicate the last tab"> <input type="text" id="new_tab_input" class="text ui-widget-content ui-corner-all" placeholder="Enter new tab name" /> </div> 

Side note: I've given a common class name for the content <div> 's to easily get the id like $(".tab").last().attr("id") , rather than getting it from the tabs which will look something like $("#tabs").find("li:last").prev().find("a").attr("href") 旁注:我为内容<div>赋予了通用的类名,以轻松获取id例如$(".tab").last().attr("id") ,而不是从标签,看起来像$("#tabs").find("li:last").prev().find("a").attr("href")

PS: Looks like jquery ui keeps on attempting to fetch the content even if # is specified for the tab's anchor. PS:看起来jquery ui继续尝试获取内容,即使为选项卡的锚点指定了# I've given it the id of dialog instead to satisfy the poor thing. 我给它提供了对话框的id ,而不是为了满足可怜的事情。

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

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