2012-02-16 133 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