how to change background-color td with jquery ?
如何用jquery更改背景颜色td?
I need to change the background-color column one in row two and three and four
我需要在第二行,第三行和第四行更改背景颜色列
my table :
我的桌子:
<table class="myTable">
<thead>
<tr>
<th>Col 1</th>
<th>Col 2</th>
<th>Col 3</th>
<th>Col 4</th>
</tr>
</thead>
<tbody>
<tr>
<td>td1</td>
<td>td2</td>
<td>td3</td>
<td>td4</td>
</tr>
<tr>
<td >td1</td>
<td>td2</td>
<td>td3</td>
<td>td4</td>
</tr>
<tr>
<td>td1</td>
<td>td2</td>
<td>td3</td>
<td>td4</td>
</tr>
<tr>
<td>td1</td>
<td>td2</td>
<td>td3</td>
<td>td4</td>
</tr>
<tr>
<td>td1</td>
<td>td2</td>
<td>td3</td>
<td>td4</td>
</tr>
</tbody>
</table>
demo : jsfiddle
演示:jsfiddle
How to do it with jQuery?
如何用jQuery做到这一点?
6 个解决方案
#1
4
You can use the nth selector in jQuery:
你可以在jQuery中使用第n个选择器:
http://api.jquery.com/nth-child-selector/
$( "table tr:nth-child(2) td:nth-child(1)" ).css("background-color", "blue");
$( "table tr:nth-child(3) td:nth-child(1)" ).css("background-color", "blue");
$( "table tr:nth-child(4) td:nth-child(1)" ).css("background-color", "blue");
$( "table tr:nth-child(5) td:nth-child(1)" ).css("background-color", "blue");
#2
1
$('.myTable tbody tr:lt(4):gt(0)')
.children('td:first-child').css('background-color', 'lightblue');
This gets the tr
with index between 0
and 4
=> 1,2,3
这将得到索引在0到4 => 1,2,3之间的tr
tr:lt(4):gt(0)
#3
1
I found:
$('table.myTable tr:gt(1):lt(3)').find('td:first').css('background-color', 'red');
$('table.myTable tr:gt(1):lt(3)')。find('td:first')。css('background-color','red');
demo : jsfiddle
演示:jsfiddle
#4
0
I would add a class to the tds you are concerned with. A good name for the class would be something that describes why you are highlighting. Then you could just add a statement in css that will handle that. Or if you want to change the color via jquery you could do the following.
我会为你关心的tds添加一个类。这个类的好名字将描述你为什么要突出显示。然后你可以在css中添加一个将处理它的语句。或者,如果您想通过jquery更改颜色,可以执行以下操作。
//Assuming highlighter is your added class name and red is the color you want to change to.
$('td.highlighter').css('background-color', 'red');
Your html for tr would look like this
你的html for tr看起来像这样
<tr>
<td class='highlighter'>td1</td>
<td>td2</td>
<td>td3</td>
<td>td4</td>
</tr>
#5
0
If would use this code :
如果要使用此代码:
$('.myTable tbody tr').filter(function(i){
return {1 : true, 2 : true, 3 : true}[i];
}).children(':first-child').css('background-color', 'red');
But of course, it would be easier to use class.
但是,当然,使用课程会更容易。
#6
0
I don't suggest using jQuery for this kind of styling because it can be a performance issue when ran and if the user has JavaScript disabled, it won't render properly.
我不建议将jQuery用于这种样式,因为它在运行时可能是性能问题,如果用户禁用了JavaScript,它将无法正常呈现。
What I suggest is using actual CSS to accomplish this task. If it's absolutely necessary to use JS for it, then adding and removing a class from an element would probably be better.
我建议使用实际的CSS来完成这项任务。如果绝对有必要使用JS,那么从元素中添加和删除类可能会更好。
Find an example of what I'm talking about below. http://jsfiddle.net/F7hnL/
找一个我正在谈论的内容的例子。 http://jsfiddle.net/F7hnL/
CSS
.myTable tr.highlight td:first-child {
background-color : red;
}
HTML
<table class="myTable">
<thead>
<tr>
<th>Col 1</th>
<th>Col 2</th>
<th>Col 3</th>
<th>Col 4</th>
</tr>
</thead>
<tbody>
<tr>
<td>td1</td>
<td>td2</td>
<td>td3</td>
<td>td4</td>
</tr>
<tr class="highlight">
<td>td1</td>
<td>td2</td>
<td>td3</td>
<td>td4</td>
</tr>
<tr class="highlight">
<td>td1</td>
<td>td2</td>
<td>td3</td>
<td>td4</td>
</tr>
<tr class="highlight">
<td>td1</td>
<td>td2</td>
<td>td3</td>
<td>td4</td>
</tr>
<tr>
<td>td1</td>
<td>td2</td>
<td>td3</td>
<td>td4</td>
</tr>
</tbody>
</table>
#1
4
You can use the nth selector in jQuery:
你可以在jQuery中使用第n个选择器:
http://api.jquery.com/nth-child-selector/
$( "table tr:nth-child(2) td:nth-child(1)" ).css("background-color", "blue");
$( "table tr:nth-child(3) td:nth-child(1)" ).css("background-color", "blue");
$( "table tr:nth-child(4) td:nth-child(1)" ).css("background-color", "blue");
$( "table tr:nth-child(5) td:nth-child(1)" ).css("background-color", "blue");
#2
1
$('.myTable tbody tr:lt(4):gt(0)')
.children('td:first-child').css('background-color', 'lightblue');
This gets the tr
with index between 0
and 4
=> 1,2,3
这将得到索引在0到4 => 1,2,3之间的tr
tr:lt(4):gt(0)
#3
1
I found:
$('table.myTable tr:gt(1):lt(3)').find('td:first').css('background-color', 'red');
$('table.myTable tr:gt(1):lt(3)')。find('td:first')。css('background-color','red');
demo : jsfiddle
演示:jsfiddle
#4
0
I would add a class to the tds you are concerned with. A good name for the class would be something that describes why you are highlighting. Then you could just add a statement in css that will handle that. Or if you want to change the color via jquery you could do the following.
我会为你关心的tds添加一个类。这个类的好名字将描述你为什么要突出显示。然后你可以在css中添加一个将处理它的语句。或者,如果您想通过jquery更改颜色,可以执行以下操作。
//Assuming highlighter is your added class name and red is the color you want to change to.
$('td.highlighter').css('background-color', 'red');
Your html for tr would look like this
你的html for tr看起来像这样
<tr>
<td class='highlighter'>td1</td>
<td>td2</td>
<td>td3</td>
<td>td4</td>
</tr>
#5
0
If would use this code :
如果要使用此代码:
$('.myTable tbody tr').filter(function(i){
return {1 : true, 2 : true, 3 : true}[i];
}).children(':first-child').css('background-color', 'red');
But of course, it would be easier to use class.
但是,当然,使用课程会更容易。
#6
0
I don't suggest using jQuery for this kind of styling because it can be a performance issue when ran and if the user has JavaScript disabled, it won't render properly.
我不建议将jQuery用于这种样式,因为它在运行时可能是性能问题,如果用户禁用了JavaScript,它将无法正常呈现。
What I suggest is using actual CSS to accomplish this task. If it's absolutely necessary to use JS for it, then adding and removing a class from an element would probably be better.
我建议使用实际的CSS来完成这项任务。如果绝对有必要使用JS,那么从元素中添加和删除类可能会更好。
Find an example of what I'm talking about below. http://jsfiddle.net/F7hnL/
找一个我正在谈论的内容的例子。 http://jsfiddle.net/F7hnL/
CSS
.myTable tr.highlight td:first-child {
background-color : red;
}
HTML
<table class="myTable">
<thead>
<tr>
<th>Col 1</th>
<th>Col 2</th>
<th>Col 3</th>
<th>Col 4</th>
</tr>
</thead>
<tbody>
<tr>
<td>td1</td>
<td>td2</td>
<td>td3</td>
<td>td4</td>
</tr>
<tr class="highlight">
<td>td1</td>
<td>td2</td>
<td>td3</td>
<td>td4</td>
</tr>
<tr class="highlight">
<td>td1</td>
<td>td2</td>
<td>td3</td>
<td>td4</td>
</tr>
<tr class="highlight">
<td>td1</td>
<td>td2</td>
<td>td3</td>
<td>td4</td>
</tr>
<tr>
<td>td1</td>
<td>td2</td>
<td>td3</td>
<td>td4</td>
</tr>
</tbody>
</table>