2012-02-16 131 views
0

我有一個表,它的TD值顯示爲jQuery的應用CSS樣式

<table> 
<tr> 
<td><?=$something?></td> 
<td><?=$something?></td> 
<td><?=$something?></td> 
<td><?=$something?></td> 
</tr> 
<tr> 
<td><?=$somethingelse?></td> 
<td><?=$somethingelse?></td> 
<td><?=$somethingelse?></td> 
<td><?=$somethingelse?></td> 

</tr> 

我想某些條件得到滿足之後,第二行中的所有TD申請jQuery的,舉例來說,第一somethingelse在第一個td小於零。

+3

凡本'<?='服務器標記結束? – 2012-02-16 09:22:30

+0

那麼你有什麼問題?選擇第二行中的單元格?在第一行找到你所在的細胞?獲取這些單元格的內容?進行比較? – Quentin 2012-02-16 09:23:28

回答

0

你可以這樣做:

 

if(yourCondition) { 
$('table > tbody > tr:eq(2) > td').each(function() { 
    $(this).css("color", "red"); //add color red for example 
}); 
} 
 

你的意思是這樣的..

+0

什麼是'>'標記? – 2012-02-16 09:43:39

+0

@JohnSmith這是直接的祖先選擇器...意思是你選擇了直接在DOM中的tr中的td – 2012-02-16 09:56:02

+0

@john你提到的條件是什麼?在這個答案是fullfilled? – 2012-02-16 10:04:28

0

$('tr:nth-child(2) td')這將選擇第二行

0

的TDS這會爲你做它。

//get the second table row (eq is zero-indexed) 
var second_tr = $('tr').eq(1); 
//get the contents of the first element in second row 
var first_td_val = second_tr.find('td:first').text();​ 
if(first_td_val < 0){ 
    //do something with the second table row. 
    second_tr.css('background-color', '#bada55'); 
} 
​ 

參見This jsFiddle