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

发布于 2024-11-05 03:52:45 字数 2438 浏览 0 评论 0原文

下面是我的表格,其中填充了 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"
});*/
}

这是我的表格

<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> 

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

Below is my table that is getting populated with spry dataset

Here is my dataset

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

Here is my jquery inside a method that is called on a button click

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"
});*/
}

Here is my 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">  {name}</td> 
         <td width="22%" align="center">  {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">  {name}</td> 
         <td width="22%" align="center">  {country}</td> 

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

Am I going wrong somewhere, please guide me. Thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

§普罗旺斯的薰衣草 2024-11-12 03:52:45

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

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

If I remember right, <tr> is only describing structure. <td> represents visual part of the table. Or this is how some browsers renders them.

Therefore $("#trEven td").css("background-color", "red") should work. And preferrably you should use classes instead of ids in these kind of cases where there may exist multiple instances.

飞烟轻若梦 2024-11-12 03:52:45

适用于我(jsFiddle)。您遇到什么问题?

如果您使用类而不是 id,则可以使用类似以下内容的内容:

$('.trEven').each(function() {
    $(this).css({"background-color": "red"});
});

请参阅参考: jQuery API - .each( )

Works for me (jsFiddle). What problems are you experiencing?

If your use classes instead of id's, you can use something like the following:

$('.trEven').each(function() {
    $(this).css({"background-color": "red"});
});

See for reference: jQuery API - .each()

罪#恶を代价 2024-11-12 03:52:45

您不应该对奇数行和偶数行使用idid 值在页面内是唯一的。

所以,我建议:

<tr class="trOdd"

然后:

<tr class="trEven"

然后:

$(".trEven")

如果您真的只想让表格主体中的第一行获得红色背景(而不是所有偶数背景),那么您的选择器应该是:

$("#tableDg tbody tr:first")

You shouldn’t be using ids for odd and even rows. id values are meant to be unique within the page.

So, I’d suggest:

<tr class="trOdd"

and:

<tr class="trEven"

and then:

$(".trEven")

If you really only want the first row in the table body to get a red background (as opposed to all the even ones), then your selector should be:

$("#tableDg tbody tr:first")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文