属性 行在类型 HTMLElement 上不存在循环时出现错误 TS2339 table
Property rows does not exist on type HTMLElement error TS2339 when loop through table
问题
Property rows does not exist on type HTMLElement error TS2339
循环时 table
关于 angular 7
我正在 angular 7
当循环到 table html 并编译我得到错误
ERROR in src/app/Pages/part-compare/part-compare.component.ts(25,38):
error TS2339: Property 'rows' does not exist on type 'HTMLElement'.
src/app/Pages/part-compare/part-compare.component.ts(27,26): error TS2339:
Property 'rows' does not exist on type 'HTMLElement'.
请问如何解决这个错误?
我尝试过的:
var table = document.getElementById("CompareParts");
for (var i = 1, row; row = table.rows[i]; i++) {
for (var j = 0, col; col = row.cells[j]; j++) {
var Cell = table.rows[i].cells
this.ACells.push(Cell);
}
}
this.length=this.ACells.length
for (var x = 0; x < this.ACells.length; x++) {
this.ARows.push(this.ACells[x]);
当你调用 document.getElementById
TypeScript 并不知道元素的具体类型是什么,所以函数 returns 泛型 HTMLElement。您应该将其转换为 HTMLTableElement。
尝试这样的事情:
var table = document.getElementById("CompareParts") as HTMLTableElement;
问题
Property rows does not exist on type HTMLElement error TS2339
循环时 table 关于 angular 7
我正在 angular 7 当循环到 table html 并编译我得到错误
ERROR in src/app/Pages/part-compare/part-compare.component.ts(25,38):
error TS2339: Property 'rows' does not exist on type 'HTMLElement'.
src/app/Pages/part-compare/part-compare.component.ts(27,26): error TS2339:
Property 'rows' does not exist on type 'HTMLElement'.
请问如何解决这个错误?
我尝试过的:
var table = document.getElementById("CompareParts");
for (var i = 1, row; row = table.rows[i]; i++) {
for (var j = 0, col; col = row.cells[j]; j++) {
var Cell = table.rows[i].cells
this.ACells.push(Cell);
}
}
this.length=this.ACells.length
for (var x = 0; x < this.ACells.length; x++) {
this.ARows.push(this.ACells[x]);
当你调用 document.getElementById
TypeScript 并不知道元素的具体类型是什么,所以函数 returns 泛型 HTMLElement。您应该将其转换为 HTMLTableElement。
尝试这样的事情:
var table = document.getElementById("CompareParts") as HTMLTableElement;