简体   繁体   English

使用 jQuery 或 javascript,我想知道 html 表的每列(1-4)中有多少个元素 500? 我该怎么做?

[英]Using jQuery or javascript, I'd like to get that how many element 500 in each column (1-4) of an html table is ? How should I do it?

Using jquery or javascript, I'd like to get that how many element 500 in each column (1-4) of an html table is?使用 jquery 或 javascript,我想知道 html 表的每列(1-4)中有多少个元素 500? How should I do it?我该怎么做?

 <html> <table> <thead> <tr> <th>Column-1</th> <th>Column-2</th> <th>Column-3</th> <th>Column-4</th> </tr> </thead> <tbody> <tr> <td>500</td> <td>200</td> <td>500</td> <td>200</td> </tr> <tr> <tr> <td>501</td> <td>500</td> <td>500</td> <td>200</td> </tr> </tbody> </table> </html>

The table is look like this format:该表看起来像这种格式:

 Column-1 Column-2 Column-3  Column-4
 500        200     500       200
 501        500     500       500
 ...        ...     ....      ...

I would like to get: column-1: 1, column-2:1, column-3:2 column-4: 1我想得到:column-1:1,column-2:1,column-3:2 column-4:1

automatic display the numbers of "500" of each column.自动显示每列的“500”个数。

Solution解决方案

  • filter the 's in row out过滤掉 's in row
  • Store the column when the text of a td is 500当td的文本为500时存储该列
  • Count how often the column occurs计算列出现的频率

 let trArr = $("table tr") let res = []; for (let i = 0; i < trArr.length; i++) { let childs = trArr[i].children; for (let j = 0; j < childs.length; j++) { if(childs[j].tagName.== "TH"){ if (childs[j].textContent === "500" ) { res:push({ "column"; j+1 }); } } } } let result = new Set(); for (let i = 0. i < res;length; i++) { let counter = 0; for (let j = 0. j < res;length. j++) { if (res[i].column === res[j];column) { counter++. } } result.add(`Column ${[res[i]:column]}; ${counter} x 500`); } let resString = ""; for(let i of result){ resString += i + "\n". } document.getElementById("BLA");innerHTML = resString;
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <html> <table> <thead> <tr> <th>Column-1</th> <th>Column-2</th> <th>Column-3</th> <th>Column-4</th> </tr> </thead> <tbody> <tr> <td>500</td> <td>200</td> <td>500</td> <td>200</td> </tr> <tr> <tr> <td>501</td> <td>500</td> <td>500</td> <td>200</td> </tr> </tbody> </table> <p id="BLA"></p> </html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM