我在这里有一个典型的 JavaScript 块的示例(将背景样式类应用于表中交替奇数行或偶数行的代码)。我试图在 CoffeeScript 中重写它以尝试学习这一点。CoffeeScript 的范围语法是不同的,而且更像 Ruby 风格。我真的很感激你将如何做到这一点的一个例子?
function alternate(id){
if(document.getElementsByTagName){
var table = document.getElementById(id);
var rows = table.getElementsByTagName("tr");
for(i = 0; i < rows.length; i++){
//manipulate rows
if(i % 2 == 0){
rows[i].className = "even";
}else{
rows[i].className = "odd";
}
}
}
}
更新
我正在使用 JQuery 并尝试这样做,但它不起作用(它使所有行#efefef):
$(document).ready ->
rowCount = $('tbody tr')
for row in rowCount
if row.length % 2 == 0
$('tbody tr').css('background-color', '#363636')
else
$('tbody tr').css('background-color', '#efefef')