2012-12-03 43 views
1

我想將類應用於tr,具體取決於表中的td單元值。如何添加一個tr的css屬性取決於td

我的HTML

<table class="k-focusable"> 
<tr> 
    <th>Name</th> 
    <th>Favorite color</th> 
<th> status</th> 
</tr> 
    <td>James</td> 
    <td>red</td> 
<td>false</td> 
</tr> 
<tr> 
    <td>John</td> 
    <td>blue</td> 
    <td>true</td> 
</tr> 
</table> 

var cellIndexMapping = { 2: true }; 
$("table.k-focusable tbody tr").each(function (rowIndex) { 
      $(this).find("td").each(function (cellIndex) { 
       if (x[cellIndex]) { 
        var c = $(this).text(); 
        if (($.trim(c) == "false") || ($.trim(c) == "null")) { 
         $("table.k-focusable tbody tr").find("td").addClass("hover"); 
        } 
       } 
      }); 
     }); 

.hover 
{ 
font-weight:bold 
} 
當我這樣做

,每一行是有這個類hover.But我想,類如果TD值爲false或null時只添加。

回答

2

試試這個

$("table.k-focusable tbody tr").each(function(rowIndex) { 
    var $td = $(this).find("td:eq(2)"); 
    var c = $td.text(); 
    if (($.trim(c) == "false") || ($.trim(c) == "null")) { 
     $td.closest('tr').addClass("hover"); 
    } 
});​ 

Check Fiddle

你不需要第二個$.each首先迭代td。 然後您可以參考td你想改變這個

+0

謝謝,但我想添加該類到tr。 – Findings

+1

@Findings/..檢查編輯後 –

5

試試這個

$("table.k-focusable tbody tr td:contains('false')") 
+0

我想只應用這個類到第三列。 – Findings

0

在您的測試if (($.trim(c) == "false") ...,你選擇的所有td。你只需要在類應用到當前單元格,因爲你已經在一個循環中通過細胞:

$(this).addClass("hover"); 
相關問題