[英]I am writing a Javascript function to to color every other row in a table a different color. Why doesn't my code work?
My understanding of the function I've written is that any table will be subject to this function through the getElementsByTag selector. 我对我编写的函数的理解是任何表都将通过getElementsByTag选择器来处理这个函数。
Then, the row count is detected to allow for traversal in the for loop. 然后,检测行计数以允许for循环中的遍历。
Then the current row is defined by "row" and if the current i value is odd, the row will be colored red. 然后当前行由“行”定义,如果当前i值为奇数,则该行将显示为红色。
But that is not what is happening so I was hoping for some insights, please? 但那不是正在发生的事情,所以我希望得到一些见解,拜托?
function tableHighlight(){
var table = document.getElementsByTagName("table");
var rowCount = table.rows.length;
for(var i=1; i<rowCount; i++) {
var row = table.rows[i];
if(i%2 != 0){
row.style.background="#000000";
}
}
}
when you use getElementsByTagName, it returns you a list 当你使用getElementsByTagName时,它会返回一个列表
function tableHighlight(){
var table = document.getElementsByTagName("table")[0];
var rowCount = table.rows.length;
for(var i=1; i<rowCount; i++) {
var row = table.rows[i];
if(i%2 != 0){
row.style.background="#000000";
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.