简体   繁体   English

Javascript获取表行TR的单元格

[英]Javascript get cells of a table row TR

I want to manipulate table cells with javascript, and so far I managed to get into tr-s, by using: 我想用javascript操纵表格单元格,到目前为止我设法进入tr-s,使用:

var as = document.getElementById('answers['+id+']');
var trs = as.getElementsByTagName("tr");
for (var i in trs)
{ ... }

UPDATE: The as is the table. 更新:就像桌子一样。

Inside of the for cycle I wanted to go on the same login line and I have tried: 在for循环中,我想要使用相同的登录行,我尝试过:

var tds = trs[i].getElementByTagName("td");

The problem is that on debugging I get the following error: 问题是在调试时出现以下错误:

Uncaught TypeError: Object #<HTMLTableRowElement> has no method 'getElementByTagName' .

How could I get into the td-s of the tr-s? 我怎么能进入tr-s的td-s?

its getElementsByTagName and not getElementByTagName 它的getElementsByTagName而不是getElementByTagName

var as = document.getElementById('answers['+id+']');
for(var i=0;i<as.rows.length;i++) {
    var trs = as.getElementsByTagName("tr")[i];
    var cellVal=trs.cells[0]
}

the variable cellVal will give the reference to the first cell or <td> similarly do for all the cells by increasing the index or putting it in a loop would make it dynamic... 变量cellVal将提供对第一个cell的引用,或<td>同样通过增加索引或将其置于循环中对所有单元格进行操作会使其动态化...

Check spelling of the method you are using to get td ie getElementByTagName 检查您正在使用的方法的拼写,以获取td,即getElementByTagName

It should be getElementsByTagName . 它应该是getElementsByTagName This should work fine as the way you are trying to fetch. 这应该像您尝试获取的方式一样正常。

Check Manipulating the table with DOM and CSS for more info. 检查使用DOM和CSS操作表以获取更多信息。

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

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