简体   繁体   中英

Click event on newly created UL LI element

I really need the help of a code guru or expert on this one.

What I can't figure out is why in my existing code below, once a new tab has been created, after calling the add_new_tab() function that, when I change my active tab to another tab , and then click back on the newly created tab, that I see that the active class does not get applied to the LI as well as the tab content doesn't get switched.

I am tearing the hair out of my head trying to figure this one out when everything else works as it should (just not with the newly created tab).

Here is my html markup:

<!DOCTYPE html>

<html>

<head>

<script type="text/javascript" src="jquery-1.12.4.js"></script>

<style type="text/css">
ul.tabs {
    margin: 0;
    padding: 0;
    float: left;
    list-style: none;
    height: 32px;
    border-bottom: 1px solid rgb(109,109,109);
    border-left: 1px solid rgb(109,109,109);
    width: 100%;
    display: flex;
    position: relative;
}
ul.tabs li {
    float: left;
    margin: 0;
    padding: 0;
    height: 32px;
    line-height: 32px;
    border-top: 1px solid rgb(109,109,109);
    border-right: 1px solid rgb(109,109,109);
    border-bottom: 1px solid rgb(109,109,109);
    border-left: none;
    margin-bottom: -1px;
    background: #e0e0e0;
    overflow: hidden;
}
ul.tabs li a {
    text-decoration: none;
    color: #000;
    display: block;
    font-size: 8pt;
    padding: 0 20px;
    border: 1px solid #fff;
    outline: none;
}
ul.tabs li a:hover {
    background: #ccc;
}   
html ul.tabs li.active, html ul.tabs li.active a:hover  {
    background: #fff;
    border-bottom: 0;
    font-weight: bold;
}
.tab_container {
    border: 1px solid rgb(109,109,109);
    border-top: none;
    clear: both;
    float: left; 
    width: 100%;
    background: #fff;
    width: 100%;
    height: 500px;
   padding: 2px;
}
.tab_wrapper {
    background: rgb(231,231,226);
    height: 100%;
}
.tab_content {
    padding: 10px;
}

</style>

<script type="text/javascript">
$(document).ready(function() {

    init_form()

});



function init_form() {

    //INITIALIZE TABS
    $(".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").on('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;       
    });



}

function add_new_tab() {

    var num_tabs = $("ul.tabs li").length

    var fileno =  ( $("#fileno").val() ) ? $("#fileno").val() : '(New)'

    //debug: alert(num_tabs)

    if (num_tabs == 0) {
        $(".tabs").show()
        $(".tab_container").show()
    }

    $("ul.tabs li").removeClass("active"); //Remove any previous "active" class set on any tabs

    $(".tab_content").hide(); //Hide all previous tab content

    $("ul.tabs").append("<div class='close_wrapper'><li class='active'><a href='#tab" + num_tabs + "'>"+ fileno +"</a><span class='close'></span></li></div>").show()

    $(".tab_wrapper").append("<div id='tab" + num_tabs +"' class='tab_content'>I've inserted a new tab for you</div>").hide().fadeIn() //INSERTS NEW TAB CONTENT

}
</script>

</head>

<body>

</body>

</html>

Please try using jQuery on with filter. simple use of on does not ad listener to the newly created elements.

$( "document" ).on( "click", "ul.tabs li", 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; 
    });

When inserting new elements to the DOM, you need to use event delegation . Basically instead of invoking the method on the <li/> s, you do it on a parent element. Otherwise you're just assigning the event handler to the elements already on the page :

$('ul.tabs').on('click', 'li', function() {
  ...
});

Click events are not attached for the newly created li. Try this.

$("ul.tabs").on('click', 'li' ,function() {

/* Your function code goes here. */

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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