更改tr背景颜色 [英] Change tr background-color

查看:357
本文介绍了更改tr背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的:

<tr id='<%=currentRow %>' onclick="SetBackgroundColor(this)" style="background-color:Yellow">

当我点击一行我想改变它的背景颜色,我这样做: p>

When i click on a row i want to change its background color and i did like this:

function SetBackgroundColor(rowId) 
{
     $(rowId).css("background-color", "#000000");
}

但我不知道为什么它不工作。有任何建议吗?

but i don't know why it doesn't work. Any suggestions please?

推荐答案

IE对于TR元素的背景颜色有问题。更安全的方法是在TR中设置TD和TR的背景:

IE has a problem with background colors for the TR element. A more safe way is to set background to the TD's and TH's inside the TR:

<table id="tabletest">
    <tr>
        <td>testcell</td>
    </tr>
</table>

<script>
$('#tabletest tr').bind('click', function(e) {
    $(e.currentTarget).children('td, th').css('background-color','#000');
})
</script>

已添加:您可以为整个表格分配单个事件处理程序以提高性能:

Added: you can assign a single event handler for the entire table to increase performance:

$('#tabletest').bind('click', function(e) {
    $(e.target).closest('tr').children('td,th').css('background-color','#000');
});

这篇关于更改tr背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆