简体   繁体   English

我正在编写一个Javascript函数来为表中的每一行着色不同的颜色。 为什么我的代码不起作用?

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

相关问题 我尝试在共享点上突出显示我的表行,如果它与数组中的任何内容匹配,为什么不起作用? - I am trying to highlight my table row on sharepoint if it matches anything in an array, why doesn't it work? 为什么当我尝试将函数的返回值与字符串连接时我的代码不起作用? - why my code doesn't work when I am trying to concatenate a function's return value with a string? 使我的html元素具有不同的背景颜色。 - Animating my html element to a different background color. 我不明白为什么我的代码不起作用有人可以检查一下吗? 我很新 (JavaScript) - i can't understand why my code doesn't work can someone please check it out? I am very new (JavaScript) 为什么我的JavaScript代码不起作用 - Why my javascript code doesn't work 为什么渐变色的过渡不起作用? - Why doesn't my transition for gradient color work? 我想用不同颜色的矩形填充画布,为什么不起作用?仅填充一种颜色 - I want to fill canvas with rectangle of different colors, why this doesn't work?It is filled with only one color 为什么我的代码中没有颜色值刷新? - Why doesn't color value refresh in my code? 为什么我的javascript在这个while循环中没有改变bg颜色? - Why doesn't my javascript change the bg color in this while loop? 为什么我的混色功能无法按预期工作? - Why won't my color mixing function work as expected?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM