[英]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.