jQuery click on Appended Element not working

后端 未结 5 930

I have an array. I am getting data from Array and using it in jQuery Append to list. But when I am clicking on list item its only showing the last element.

var a         


        
5条回答
  •  失恋的感觉
    2021-01-29 05:16

    var array = [[1,2,7],[3,4,8],[5,6,9]];
    for (var i = 0; i < array.length; i++){
        var firstVal = array[i].[0];
        var secondVal = array[i].[1];
        var thirdVal = array[i].[2];
    
        var list = "
    "+ firstVal +"

    " + "

    "+ secondVal +"

    "; // change here $("#myDiv").append(list.cloneNode(true)); // Clone the node here $(".divClass").on('click', function(){ alert(document.getElementsByClassName("thv"+i)[0].innerHTML); // change here }) }

    If you didn't clone a node before append, the same node will be added. If you cloned it, it will create a copy every time. Now check it. Also there is a problem that you are alerting thirdVal. It will store the last value. So, create an object with style display:none and add third value to it. Then use alert(.innerHTML);

提交回复
热议问题