简体   繁体   English

如何动态添加新标签?

[英]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: 我想提出一个函数,只需单击一下按钮,便可以动态地进行以下操作:

  1. If there are no tabs in my ul.tabs, create, and add one., then select it and display its content. 如果我的ul.tabs中没有选项卡,请创建并添加一个。,然后选择它并显示其内容。

  2. 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.

相关问题 JavaScript中的JSON数据如何动态添加新级别? - How can I dynamically add a new level to JSON data in JavaScript? 如何打开新窗口/选项卡并动态填充内容? - How do I open a new window/tab and dynamically fill it with content? 如何动态地将选项卡“ALL”添加到菜单选项卡 - How to add tab "ALL" to menu tab dynamically 如何在新标签页中打开的外部页面添加“后退”按钮? - How can I add a 'back' button to external page opened in new tab? 在新窗口/标签页中打开链接后,如何向页面添加内容? - How can I add content to a page after opening the link in a new window/tab? 如何获取用户输入并将其添加到 url 并在新选项卡中打开 url? - How can I get a User input and add it in a url and open the url in a new tab? 如何在循环内动态地向现有的 Javascript 对象添加新的键:值对 - How can I add a new key:value pair to an existing Javascript object dynamically within a loop 每当数组长度增加时,如何动态地向页面添加新内容? - How can I dynamically add new content to my page each time my array length increases? 如何将值从现有对象动态添加到新对象? - How can I add values from existing object to new object dynamically? 我可以通过React在JSX中动态添加新的HTML标签输入吗 - Can I add dynamically new Html tag input in JSX by React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM