[英]Loop through the 1st row of HTML table (GridView) and get the cell content using Javascript
I have a GridView which reads from a file and when I inspect element its HTML table looks something like this : 我有一个GridView从文件中读取,当我检查元素时,其HTML表如下所示:
<div> <table> <tbody> <tr> <th id= "header1">...</th> <th id= "header2">...</th> <th id= "header3">...</th> <th id= "header4">...</th> <th id= "header5">...</th> <tr>..</tr> <tr>..</tr> <tr>..</tr> <tr>..</tr> </table> </div>
I need to loop through the first row to be able to get the header values using javascript. 我需要遍历第一行才能使用javascript获取标头值。 I can't seem to loop through just the first row to extract values.
我似乎无法仅遍历第一行来提取值。
I understand that I should count the number of columns first to create my "for" loop but I am only able to count my rows. 我知道我应该先计算列数才能创建“ for”循环,但我只能计算行数。
var table = document.getElementById("table1");
var rowCount = table.rows.length;
for (var r = 0, n = table.rows.length; r < n; r++) {
There's nothing like table.columns.length.How do I do that? 没有什么像table.columns.length。我该怎么做? Any help would be greatly appreciated.
任何帮助将不胜感激。
like that you will get header
values in an array 这样,您将在数组中获取
header
值
const el = [...document.querySelectorAll("th")]; const res = el.map(el => el.innerHTML) console.log(res)
<div> <table> <tbody> <tr> <th id="header1">TEXT1</th> <th id="header2">TEX2</th> <th id="header3">TEXT3</th> <th id="header4">TEXT4</th> <th id="header5">TEXT5</th> </tr> <tr>..</tr> <tr>..</tr> <tr>..</tr> <tr>..</tr> </table> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.