简体   繁体   English

我正在尝试在 html 中添加一个 for 循环,? 但 for 循环显示错误

[英]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>`

enter image description here在此处输入图像描述

enter image description here在此处输入图像描述

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.

相关问题 我正在处理html 5画布并尝试在其上绘制圆圈,但是当我尝试使用循环时,所有内容都消失了 - I am working on an html 5 canvas and trying to draw circles on it, but as I trying to use a loop all disappears 我一直在尝试通过用户输入使用 for 循环,但由于某种原因,它既不显示错误也不显示 output - I've been trying to use for loop via user input but for some reason it neither shows the error nor shows the output 我想在示例HTML中添加Google广告感,但在控制台中显示错误? - I m trying to add google ad sense in a sample html but it shows error in the console? 我正在尝试进行while循环,但是循环是无限的,我不确定为什么 - I am trying to conduct a while loop but the loop is infinite and I am unsure why 在循环错误内向表添加行 - Add row to table inside a loop error 我正在尝试使用 aa for 循环将 object 键值添加到空数组进行过滤,我不断得到一个空数组 - I am trying to add an object key value to an empty array using a a for loop to filter, I keep getting an empty array 我试图遍历对象数组并将其填充到表中 - I am trying to loop through an array of objects and populate them to a table 如何循环浏览页面? 我正在尝试 web 抓取页面 - How to loop through the pages? I am trying to web scrape the page JavaScript循环内的HTML - HTML inside of a JavaScript loop 在for循环内生成html - Generating html inside for loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM