简体   繁体   English

循环浏览HTML表的第一行(GridView),并使用Javascript获取单元格内容

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

相关问题 如何使用 Google Apps 脚本遍历范围,获取第一个 Col 数据并继续迭代以形成 html 表? - How to iterate through a range, get 1st Col data once and continue iterating to form an html table, using Google Apps Script? 如何使用PHP偏移HTML表以从不同列的第二行单元格值中减去第一行的单元格值 - How To offset an HTML table with PHP to Subtract cell value of 1st row from 2nd row cell value of different columns 如何使用javascript连续获取HTML表格单元格上的Dropdownlist值 - How to get Dropdownlist value on HTML Table Cell in a Row using javascript 始终在屏幕上显示表格的第一行和第一列 - Always display 1st row and 1st column of a table on screen 使用javascript中的id循环遍历html内容 - Loop through html content using the ids in javascript 使用javascript遍历html表单元 - loop through the html table cells using javascript 限制数据表的第一个表行 - Restrict 1st table row for datatables 如何使用javascript javascript获取HTML表格单元格的内容,具体取决于复选框 - how to get content of html table cell using javascript javascript depending on checkbox Javascript中第一行列的getElementsByTagName - getElementsByTagName for the 1st row columns in Javascript 遍历html表并获取javascript中每一行的行号 - iterate through a html table and get row number for each row in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM