[英]How can I add a new tab dynamically?
I need your help, 我需要你的帮助,
My request is two fold. 我的要求是两折。 I'd like to come up with a function to be able, at the click of a button, to be able to dynamically: 我想提出一个函数,只需单击一下按钮,便可以动态地进行以下操作:
If there are no tabs in my ul.tabs, create, and add one., then select it and display its content. 如果我的ul.tabs中没有选项卡,请创建并添加一个。,然后选择它并显示其内容。
If there are already any tabs in my ul.tabs to add a new tab to the next tab., select it, and display its contents. 如果我的ul.tabs中已经有任何标签可将新标签添加到下一个标签,请选择它并显示其内容。
Here's what I have so far, but its far from anything dymanic: 到目前为止,这是我所拥有的东西,但远没有任何动态变化:
$(document).ready(function() {
//Default Action
$(".tab_content").hide(); //Hide all content
$("ul.tabs li:first").addClass("active").show(); //Activate first tab
$(".tab_content:first").show(); //Show first tab content
//On Click Event
$("ul.tabs li").click(function() {
$("ul.tabs li").removeClass("active"); //Remove any "active" class
$(this).addClass("active"); //Add "active" class to selected tab
$(".tab_content").hide(); //Hide all tab content
var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
$(activeTab).fadeIn(); //Fade in the active content
return false;
});
});
<div id="main" style="visibility: visible; display: inline-block;">
<ul class="tabs">
<li><a href="#tab1">XAL-2107-482336</a></li>
<li><a href="#tab2">A-2017-00471</a></li>
</ul>
<div class="tab_container">
<div class="tab_wrapper">
<!--BEGIN DIV TAB 1 -->
<div id="tab1"></div>
<!--END DIV TAB 1 -->
<!--BEGIN DIV TAB 2 -->
<div id="tab2"></div>
<!--END DIV TAB 2 -->
</div><!-- END DIV tab_wrapper -->
</div><!-- END DIV tab_container -->
</div><!-- END DIV main -->
1. hide .tab_wrapper
the inner div 1.隐藏.tab_wrapper
内部div
original: 原版的:
$(".tab_content").hide();
edit: 编辑:
$(".tab_wrapper").find("div").hide();
2. get the first child id, and show tab content 2.获取第一个子ID,并显示标签内容
original: 原版的:
$(".tab_content:first").show();
edit: 编辑:
var tab_id = $("ul.tabs li:first a").attr("href");
$(tab_id).show();
3. hide .tab_wrapper
the inner div, and show tab content 3.隐藏.tab_wrapper
内部div,并显示选项卡内容
original: 原版的:
$(".tab_content").hide();
var activeTab = $(this).find("a").attr("href");
edit: 编辑:
$(".tab_wrapper").find("div").hide();
var activeTab = $(this).find("a").attr("href");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.