首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >遍历html数组在javascript中不起作用

遍历html数组在javascript中不起作用
EN

Stack Overflow用户
提问于 2018-08-31 08:33:13
回答 1查看 70关注 0票数 2

我试着遍历它,但是失败了。下面的函数orderpackage (){

代码语言:javascript
复制
var product_line = $('#carttable')[0].getElementsByTagName('tr');

for (var i = 1; i < product_line.length; i++) {
}

console.log(product_line)

在控制台输出中生成以下内容

每一个tr.table_row都是由这个

代码语言:javascript
复制
<tr class="table_row">
  <td class="column-1">
    <div class="how-itemcart1">
      <img src=${Items.imageurl} alt="IMG">
    </div>
  </td>
  <td class="column-2">${Items.name}</td>
  <td class="column-3">$ ${Items.price}</td>
  <td class="column-4">
    <div class="wrap-num-product flex-w m-l-auto m-r-0">
      <div class="btn-num-product-down cl8 hov-btn3 trans-04 flex-c-m qty-change" id="qty-change">
        <i class="fs-16 zmdi zmdi-minus"></i>
      </div>

      <input class="mtext-104 cl3 txt-center num-product" type="number" name="num-product1" value=${Items.user_quantity}>

      <div class="btn-num-product-up cl8 hov-btn3 trans-04 flex-c-m qty-change" id="qty-change">
        <i class="fs-16 zmdi zmdi-plus"></i>
      </div>
    </div>
  </td>
  <td class="column-5">${line_total_sum}</td>
</tr>

我想要做的就是遍历每个元素,得到价格、数量和行总数。当我尝试下面的函数时,行total is show error

代码语言:javascript
复制
var product_line = $('#carttable')[0].getElementsByTagName('tr')[4];

如何遍历每个tr以获得价格、数量和行合计?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-31 08:54:46

使用普通的JS,您可以使用HTMLTableElement.rows来获取表中的所有行。

然后你需要遍历它们,可能会跳过第一个(头)。对于每个表,访问包含所需字段的子项(单元格),并使用它们创建一个新数组[price, quantity, total],然后将该数组推送到包含所有表数据的另一个数组中。

以下是使用Array.from()Array.prototype.map()完成此操作的一种方法

代码语言:javascript
复制
const tableData = Array.from(document.getElementById('table').rows).slice(1).map(row => {
  const cells = row.children;
  
  return [cells[0].innerText, cells[1].innerText, cells[2].innerText];
});

console.log(JSON.stringify(tableData));
代码语言:javascript
复制
table {
  border-collapse: collapse;
  font-family: monospace;
  text-align: right;
}

table, th, td {
  border: 3px solid black;
}

th, td {
  padding: 5px 10px;
}
代码语言:javascript
复制
<table id="table">
  <tr><th>PRICE</th><th>QUANTITY</th><th>TOTAL</th></tr>
  <tr><td>1</td><td>5</td><td>5</td></tr>
  <tr><td>10</td><td>2</td><td>20</td></tr>
  <tr><td>8</td><td>4</td><td>32</td></tr>
</table>

如果您更喜欢使用for...of循环:

代码语言:javascript
复制
let tableData = [];

// Iterate over every row:
for (const row of document.getElementById('table').rows) {
  // Get the cells in the current row:
  const cells = row.children;
  
  // Read the text on the columns we want:
  tableData.push([cells[0].innerText, cells[1].innerText, cells[2].innerText]);
}

// Remove the first row (header):
tableData = tableData.slice(1);

console.log(JSON.stringify(tableData));
代码语言:javascript
复制
table {
  border-collapse: collapse;
  font-family: monospace;
  text-align: right;
}

table, th, td {
  border: 3px solid black;
}

th, td {
  padding: 5px 10px;
}
代码语言:javascript
复制
<table id="table">
  <tr><th>PRICE</th><th>QUANTITY</th><th>TOTAL</th></tr>
  <tr><td>1</td><td>5</td><td>5</td></tr>
  <tr><td>10</td><td>2</td><td>20</td></tr>
  <tr><td>8</td><td>4</td><td>32</td></tr>
</table>

或者仅仅是一个普通的for循环:

代码语言:javascript
复制
const rows = document.getElementById('table').rows;
const totalRows = rows.length;
const tableData = [];

// Iterate over every row, skipping the first one (header) already:
for (let i = 1; i < totalRows; ++i) {
  // Get the cells in the current row:
  const cells = rows[i].children;
  
  // Read the text on the columns we want:
  tableData.push([cells[0].innerText, cells[1].innerText, cells[2].innerText]);
}

console.log(JSON.stringify(tableData));
代码语言:javascript
复制
table {
  border-collapse: collapse;
  font-family: monospace;
  text-align: right;
}

table, th, td {
  border: 3px solid black;
}

th, td {
  padding: 5px 10px;
}
代码语言:javascript
复制
<table id="table">
  <tr><th>PRICE</th><th>QUANTITY</th><th>TOTAL</th></tr>
  <tr><td>1</td><td>5</td><td>5</td></tr>
  <tr><td>10</td><td>2</td><td>20</td></tr>
  <tr><td>8</td><td>4</td><td>32</td></tr>
</table>

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52107030

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档