javascript - 新创建的 UL LI 元素上的单击事件

标签 javascript jquery html html-lists

我真的需要代码大师或专家在这方面的帮助。

我不明白的是为什么在下面的现有代码中,一旦创建了新选项卡,在调用 add_new_tab() 函数后,当我将事件选项卡更改为另一个选项卡,然后单击返回时新创建的选项卡,我看到事件类没有应用于 LI,并且选项卡内容也没有切换。

当其他一切都按预期工作时(只是不使用新创建的选项卡),我正在抓狂地试图弄清楚这个问题。

这是我的 html 标记:

<!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>

最佳答案

请尝试使用带有过滤器的 jQuery。简单使用 on 不会向新创建的元素添加监听器。

$( "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; 
    });

关于javascript - 新创建的 UL LI 元素上的单击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47565554/

相关文章:

javascript - 如何从复杂的对象数组中获取唯一的对象数组?

jquery - 使用 jquery 将表单值传递到 iframe 的 src url

javascript - 如何在 win-list-view 中动态添加项目源

html - 如何制定可靠的 CSS 策略?

javascript - Angularjs 中的应用程序范围常量

JavaScript 将某些变量分配给特定范围

javascript - 如何左右滑动div内容..?

javascript - 在嵌套列表中,单击时更改单个 <li> 元素的样式

javascript - underscorejs中_.each(list)为空时如何显示消息?

c# - 标签方向