javascript - 如何使用 for 循环构建动态 html

标签 javascript html innerhtml dynamic-html

我想动态创建一个表,其中一些表具有通过循环链接到其他一些网址的链接,注意:在每个表中,我正在填充动态html,其中包含从数组填充的动态值。下面给出了我的代码。当我运行此代码时,js 返回一个数组“unexpected tocken 'for'”,我也用 while 尝试了它。错误再次相同。帮我摆脱怪胎..

html += '<tr class="' + (i % 2 == 0 ? 'grid_color' : '') + '" data-type="record"  data-record-id="' + records[i].id + '" data-row-index="' + i + '">'

+ '<td data-table-col="16" title="' + Samples.htmlEntities(records[i].name_id) + '">' +
for (i = 0; i < nameSplit.length; i++) {
    if (nameSplit[i] != "None") {
        name = nameSplit[i].split(".");
        name = name.shift();
        '<a target = "_blank" href="http://www.google.com/' + name + '">' + name.substr(0, 10 / nameSplit.length) + (name.length > 10 / nameSplit.length ? '...' : '') + '</a>'
    } else {
        if (i != nameSplit.length - 1) {
            "|";
        }
    } else {
        if (i != nameSplit.length - 1) "None".concat("|");
        else "None";
    }
}
}
'</td>'

最佳答案

我尝试修复您的代码缩进和格式,但它对我来说是无法读取的。但无论如何,我会将其作为示例,您可以根据自己的具体需求进行调整。

var html = "<table>";
for(var i = 0; i<someCondition; i++){
  html +="<tr><td>Cell 1</td><td>Cell 2</td><tr>";
}
html += "</table>";

您收到的错误是因为代码中的某个地方有类似 ... + for(...) 的内容,但它不起作用,for 循环不会返回,因此它不会从中得到任何东西。

关于javascript - 如何使用 for 循环构建动态 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22353208/

相关文章:

javascript - 在缺少图像的 JavaScript 中打印弹出窗口

javascript - 如何对 onclick 和 oninput 事件使用相同的代码

javascript - 将 RSS 提要导入 MongoDB

javascript - 使用 requestAnimationFrame 对 HTML 视频进行动画处理

html - 使用绝对位置 div 在 body 上启用滚动

javascript - 鼠标悬停事件监听器在元素内移动鼠标时触发多次,但仅在更新 innerHTML 时触发

javascript - 使用 inner.HTML 添加淡入淡出效果

JavaScript 返回错误结果

javascript - 数字输入最小属性不适用于 float

javascript - 悬停时更改内部 HTML