我尝试使用 JavaScript 更改表格中行的背景,但它不起作用。 这是我的 code .但是我试图用 html 及其工作创建一个表。 P.S : 我想使用 webkit。
代码:
var table = document.getElementById("tab");
var rowCount = table.rows.length;
for(var i=0;i<6;i++) {
row = table.insertRow(rowCount);
row.id = "row"+i;
cell1 = row.insertCell(0);
var content = document.createElement("output");
content.innerHTML = i ;
cell1.appendChild(content);
rowCount++;
}
$(document).ready(function(){
$('td').each(function(i) {
$('#tab').on('click', '#row'+i, function(){
document.getElementById("row"+i).style.background = 'white';
//alert(i);
});
});
//example2
$('#td1').on('click', function(){
document.getElementById("td1").style.background = 'white';
});
});
最佳答案
您遇到的问题是因为您在 TD
上设置背景渐变,然后设置 TR
的背景颜色。由于 TD 在 TR 内,您只是没有看到您的更改得到应用。
关于javascript - 使用 JavaScript 更改行表颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13957276/