小编rud*_*dib的帖子

Javascript只在最后一个条目的foreach循环中添加事件监听器

我有一个函数,它通过一个数组并添加<h3>元素到div.然后它将一个事件监听器(an onclick)添加到当前<h3>元素,但只有通过该函数的最后一个元素由onclick.

var runstr = [];

//txt comes from the content of a tab separated textfile spilt by '\n'
txt.forEach(function (lcb) { //lcb goes through each line of txt
    lcb = lcb.split("   ", 30); //split the line by tab
    //MainContent_Infralist is a div where the <h3> elements are listed and lcb[2] is the title
    document.getElementById("MainContent_Infralist").innerHTML = 
        document.getElementById("MainContent_Infralist").innerHTML + 
        '<h3 class="Infraa" id="' + "Infralist_" + lcb[2] + '">' + lcb[2] + '</h3>';
    //I put …
Run Code Online (Sandbox Code Playgroud)

javascript asp.net loops event-listener

0
推荐指数
1
解决办法
805
查看次数

标签 统计

asp.net ×1

event-listener ×1

javascript ×1

loops ×1