我會使用一個循環(如果合併單元格將始終保持不變..
看到小提琴: http://jsfiddle.net/jFIT/k5yZ9/4/
$("table tr:not(:first)").each(function() {
if($(this).find('td').length == 7)
{
//can replace with array 2/4/6
$(this).find('td:nth-child(2)').css("background-color","#d1deec");
}
else
{
// 3/5/7
$(this).find('td:nth-child(3)').css("background-color","#d1deec");
}
//loop array
});
更新
http://jsfiddle.net/jFIT/k5yZ9/6/
使用數組:
$("table tr:not(:first)").each(function() {
var arr = [];
var $this = $(this);
if ($(this).find('td').length == 7) {
arr.push(2, 4, 6);
} else {
arr.push(3, 5, 7);
}
$.each(arr, function (ind, val) {
$this.find('td:nth-child(' + val + ')').css("background-color", "#d1deec");
});
});
你爲什麼用jQuery的造型,而不是CSS? – Albzi
由於我使用的是CMS,並且網站上有很多表格用css來設計它,所以我需要使用很多我不想要的類,所以我試圖讓它變成動態的,所以如果添加了新表格,風格自動。我希望這是有道理的 –
是的,但你可以在CSS中做到這一點? – Albzi