javascript - 更改表中第一行的背景颜色

标签 javascript css jquery spry

下面是我的表格,其中填充了 spry 数据集

这是我的数据集

var ds1 = new Spry.Data.XMLDataSet("/xml/data.xml", "rows/row"); 

这是我在单击按钮时调用的方法中的 jquery

function addRow()
{
var newRow = new Array();
var nextID = ds1.getRowCount(); 

newRow['ds_RowID'] = nextID; 
newRow['id'] = "x"; 
newRow['name'] = "Abhishek";
newRow['country'] = "India";


ds1.dataHash[newRow['ds_RowID']] = newRow; 
ds1.data.push(newRow); 

Spry.Data.updateRegion(ds1); 

ds1.sort('name','descending'); 
ds1.setCurrentRow(newRow.ds_RowID);

$(".trEven td").css("background-color", "red");
alert($.fn.jquery);

/*$("#tableDg tbody tr:first").css({
    "background-color": "red"
});*/
}

这是我的 table

<div id="cdiv" style="width:100%;" spry:region="ds1">
<table id="tableDg" 
    style="border:#2F5882 1px solid;width:100%;"  cellspacing="1" cellpadding="1"> 

    <thead>
     <tr id="trHead" style="color :#FFFFFF;background-color: #8EA4BB"> 
         <th width="2%"><input id="chkbHead" type='checkbox' /></th>
         <th width="10%" align="center" spry:sort="name"><b>Name</b></th> 
         <th width="22%" align="center" spry:sort="host"><b>Country</b></th> 

     </tr>
     </thead>

     <tbody spry:repeat="ds1">   
     <tr id="trOdd"   
     spry:if="({ds_RowNumber} % 2) != 0" onclick="ds1.setCurrentRow('{ds_RowID}');"
        style="color :#2F5882;background-color: #FFFFFF" class="{ds_OddRow}"> 
         <td><input type="checkbox" id="chkbTest" class = "chkbCsm"></input></td>
         <td width="10%" align="center">&nbsp;&nbsp;{name}</td> 
         <td width="22%" align="center">&nbsp;&nbsp;{country}</td> 


     </tr> 

     <tr id="trEven" 
     spry:if="({ds_RowNumber} % 2) == 0" onclick="ds1.setCurrentRow('{ds_RowID}');"
        style="color :#2F5882;background-color: #EDF1F5;" class="{ds_EvenRow}"> 
         <td><input type="checkbox" class = "chkbCsm"></input></td>
         <td id="tdname" width="10%" align="center">&nbsp;&nbsp;{name}</td> 
         <td width="22%" align="center">&nbsp;&nbsp;{country}</td> 

     </tr>
     </tbody>
     </table>
</div> 

我哪里出错了,请指导我。谢谢:)

最佳答案

如果我没记错的话,<tr>只是描述结构。 <td>代表表格的视觉部分。或者这就是某些浏览器呈现它们的方式。

因此$("#trEven td").css("background-color", "red")应该管用。在可能存在多个实例的情况下,最好使用类而不是 ID。

关于javascript - 更改表中第一行的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5881230/

相关文章:

Javascript 设置超时();对于 ProgressBar 不起作用

jquery - 多个 Jquery 插件不能同时工作并影响彼此的功能

javascript - 检查数字是否在数组中的最快方法?

javascript - 扩展 iframe 问题

javascript - 我怎样才能确保始终显示至少一个 DIV?

javascript - Youtube iframe 视频通过单个按钮播放/静音。单击播放/静音时还可切换图像

javascript - 将点击处理程序绑定(bind)到除特定 HTML 标记之外的所有元素

javascript - 在 onkeyup 事件中更改值时不会触发 onchange 事件

javascript - 缩小的粘性导航

javascript - AngularJS : Asynchronously initialize filter