[英]jQuery alternate table row colors for a specific table
我有兩張桌子。 <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的表着色。 如何實現?
修改你的代碼,如:
$(document).ready(function()
{
$("table#id2 tr:even").css("background-color", "#F4F4F8");
$("table#id2 tr:odd").css("background-color", "#EFF1F1");
});
這假設您將表的id
設置為id2
例如:
<table id="id2">
首先, 它不允許以數字開頭的id 。 將表更改為具有以下內容的ID:
<table id="table1"></table>
然后,您需要做的就是將正確的選擇器添加到您的jQuery中:
$("#table2 tr:even").css(...);
$("#table2 tr:odd").css(...);
Pass your table Id as parameter:
settingTableRowColor('tableid');
var settingTableRowColor = function(tableName){
$('#'+tableName+' tr:odd').attr('class','odd');
$('#'+tableName+' tr:even').attr('class','even');
}
$("#tab1 tr:odd").css('background-color', 'red');
也有效
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.