Javascript 使用不同颜色替换表行
使用以下代码,我生成一个表,用json文件中的数据填充它:Javascript 使用不同颜色替换表行,javascript,html,jquery,css,html-table,Javascript,Html,Jquery,Css,Html Table,使用以下代码,我生成一个表,用json文件中的数据填充它: $(function() { $.getJSON("Api/TabellaTempoMedio.php", function(data) { $.each(data, function(index, row) { $("#result").append("<tr>"); $.each(row, f
$(function() {
$.getJSON("Api/TabellaTempoMedio.php", function(data) {
$.each(data, function(index, row) {
$("#result").append("<tr>");
$.each(row, function(index2, column) {
$("#result").append("<td>" + column + "</td>");
});
$("#result").append("</tr>");
});
});
});
我该怎么办?
。表1{
背景:红色;
}
.表1 tr{
背景:蓝色;
}
.表1 tr:第n个子项(2n+1){
背景:黄色;
}
数据
数据
数据
数据
“它不工作”不是一个有用的问题陈述。编辑你的问题,包括你如何知道它不起作用的描述(预期与实际行为)。如果不将此代码所针对的HTML视为一个整体,则几乎不可能确切地说明为什么此操作不起作用。使用该css代码,行不会交替,并且没有任何更改您的追加过程是不正确的。将行追加到表中,然后将单元格追加到表中时,该单元格不会插入行中。不能像在代码编辑器中编写html那样插入结束标记。DOM只理解完整的元素
<table>
<thead>
<tr>
<th>Field1</th>
<th>Field2</th>
</tr>
</thead>
<tbody id="result">
</tbody>
</table>
tr:nth-child(odd) { background-color: grey; }