我想动态创建一个表,其中一些表具有通过循环链接到其他一些网址的链接,注意:在每个表中,我正在填充动态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/