[英]I am trying to add a for loop inside html, ? but the for loop shows error
I see a red squiggly line under { while add this for loop in my table, what am I missing here.我在 { 下看到一条红色波浪线,而在我的表中添加这个 for 循环,我在这里缺少什么。
var cartHtml = `<table>
<tr>
<th> Item</th>
<th> Description</th>
<th> Price</th>
<th> Qty</th>
<th> Totall</th>
</tr>
<tr>`
${for (var i=0; i<5; i++){
`<td>data</td>`
}}
`</tr>
</table>`
Instead of trying to do a for-loop directly in innerHTML you can make a function of the loop and save the return data you want in another variable:除了尝试直接在 innerHTML 中执行 for 循环之外,您还可以创建循环的 function 并将所需的返回数据保存在另一个变量中:
let loopdata;
const loop = () => {
for (let i = 0; i < 5; i++) {
loopdata+=`<td>data</td>`;
}
return loopdata;
};
var cartHtml = `
<table>
<tr>
<th> Item</th>
<th> Description</th>
<th> Price</th>
<th> Qty</th>
<th> Totall</th>
</tr>
<tr>
${loop()}
</tr>
</table>`;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.