jQuery特定表的备用表行颜色

时间:2010-05-24 13:54:05

标签: javascript jquery

我有两张桌子。 <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");

});

这两个表都得到了备用的行颜色,我不想要,我只想为id = 2的表着色。如何实现?

4 个答案:

答案 0 :(得分:17)

修改您的代码,如:

$(document).ready(function()
{
  $("table#id2 tr:even").css("background-color", "#F4F4F8");
  $("table#id2 tr:odd").css("background-color", "#EFF1F1");
});

这假设您将id设置为id2的表格,例如:

<table id="id2">

答案 1 :(得分:10)

首先是it's not allowed to have an id starting with a number。将表更改为具有以下内容的ID:

<table id="table1"></table>

然后,您需要做的就是将正确的selector添加到您的jQuery中:

$("#table2 tr:even").css(...);
$("#table2 tr:odd").css(...);

答案 2 :(得分:1)

Pass your table Id as parameter:


settingTableRowColor('tableid');

var settingTableRowColor = function(tableName){
    $('#'+tableName+' tr:odd').attr('class','odd');
    $('#'+tableName+' tr:even').attr('class','even');
}

答案 3 :(得分:0)

$("#tab1 tr:odd").css('background-color', 'red'); 

也有效