Javascript 特定表格的jQuery备用表格行颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2897465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery alternate table row colors for a specific table
提问by tarique
i have two tables. <table id='1'></table>and <table id='2'></table>. When i put this code:
我有两张桌子。<table id='1'></table>和 <table id='2'></table>。当我把这段代码:
$(document).ready(function()
{
//for table row
$("tr:even").css("background-color", "#F4F4F8");
$("tr:odd").css("background-color", "#EFF1F1");
});
Both the tables got it alternate row colors, which i dont want, i want only to color the table with id=2. how it can be accomplished?
两个表都得到了交替的行颜色,这是我不想要的,我只想用 id=2 为表着色。如何实现?
回答by Sarfraz
Modify your code like:
修改您的代码,如:
$(document).ready(function()
{
$("table#id2 tr:even").css("background-color", "#F4F4F8");
$("table#id2 tr:odd").css("background-color", "#EFF1F1");
});
This assumes you have table with idset to id2eg:
这假设您有id设置为id2例如的表:
<table id="id2">
回答by nickf
First thing is that it's not allowed to have an id starting with a number. Change the tables to have ids something like this:
首先,不允许以数字开头的 id。将表更改为具有以下 ID:
<table id="table1"></table>
Then, all you need to do is add the right selectorinto your jQuery:
然后,您需要做的就是将正确的选择器添加到您的 jQuery 中:
$("#table2 tr:even").css(...);
$("#table2 tr:odd").css(...);
回答by frangly
Pass your table Id as parameter:
settingTableRowColor('tableid');
var settingTableRowColor = function(tableName){
$('#'+tableName+' tr:odd').attr('class','odd');
$('#'+tableName+' tr:even').attr('class','even');
}
回答by Mohammed mansoor
$("#tab1 tr:odd").css('background-color', 'red');
also works
也有效

