簡體   English   中英

jQuery以應用css樣式

[英]jquery to apply css style

我有一個表,其td值顯示為

<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,例如,第一個td中的第一個something小於零。

您可以執行以下操作:


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

你的意思是那樣的..

$('tr:nth-child(2) td')這將選擇第二行的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');
}
​

看到這個jsFiddle

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM