我創建了使用「添加行」按鈕將行添加到表中的程序。每行都作爲新行添加到表中。我想爲奇數和偶數行添加不同的背景色。我正在使用jquery添加背景,但在我的代碼中有一點錯誤。我的意思是我可以在行中添加背景顏色,但顯示不正確。Bgcolor動態使用jquery動態使用表中的替代行
這裏是jQuery代碼;
<script>
$(document).ready(function() {
var id = 0;
// Add button functionality
$("#addrow").click(function() {
id++;
if(id%2==0){
$(".prototype td").css("background-color","#eee");
}else{
$(".prototype td").css("background-color","#ddd");
}
var master = $(this).parents("table.stdform");
// Get a new row based on the prototype row
var prot = master.find(".prototype").clone();
master.find(".fancyTable tbody").append(prot);
});
});
</script>
這裏是HTML代碼;
<html>
<head>
<title></title>
</head>
<body>
<table width="100%" cellpadding="0" cellspacing="0" border="0"
class="stdform">
..other codes...
<tr><td>
<table class="fancyTable" id="sortable-table"
cellpadding="0" cellspacing="0" width="100%">
<thead>
<tr>
<td>header one</td>
<td>header two</td>
<td>header three</td>
</tr>
</thead>
<tbody id="job-tbody">
<tr class="prototype">
<td>one</td>
<td>two</td>
<td>three</td>
</tr>
</tody>
</table>
</td></tr>
</table>
</body>
</html>
謝謝..
這是一個明顯的CSS問題 - 所以尋找斑馬錶 – mplungjan
感謝@mplungjan,我用 '$(「。fancyTable tbody tr:n-child(odd) 「).addClass(」 奇數「); (「。fancyTable tbody tr:nth-child(even)」)。addClass(「even」); –