简体   繁体   English

改变tr背景颜色

[英]Change tr background-color

I have something like this: 我有这样的事情:

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

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 has a problem with background colors for the TR element. IE有TR元素的背景颜色问题。 A more safe way is to set background to the TD's and TH's inside the TR: 更安全的方法是在TR中设置TD和TH的背景:

<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');
});

In jQuery you do not have to use the onclick attribute to assign an event handler. 在jQuery中,您不必使用onclick属性来分配事件处理程序。 Lets say you add a class called mytr to each tr that you want to affect. 假设您为要影响的每个tr添加一个名为mytr的类。 Then you can do something like this: 然后你可以做这样的事情:

 $(document).ready(function(){
        $(".mytr").click(function(){
             $(this).css("background-color", "#000000");
        });
 });

And that will apply the event handler to all rows with the class mytr. 这将把事件处理程序应用于具有类mytr的所有行。

This will reset each row upon clicking a new one... 这将在点击新行时重置每一行......

$(document).ready(function(){

  $('tr').click(function(){
    $('tr td').css({ 'background-color' : 'green'});
    $('td', this).css({ 'background-color' : 'red' });
  }); 

});

demo: http://jsbin.com/aciqi/ 演示: http//jsbin.com/aciqi/

 $('#RowID').children('td, th').css('background-color','yellow');

A simpler solution is to probably use a selector for all rows in the table or addClass. 一个更简单的解决方案是可能对表或addClass中的所有行使用选择器。

Example

$("#myTable tr").click(function() {
    $(this).css('background-color', '#f00');
});

or 要么

$("#myTable tr").click(function() {
    $(this).addClass('selected');
});

Instead of changing the table row background color, try changing the table cell background color. 请尝试更改表格单元格背景颜色,而不是更改表格行背景颜色。

$(document).ready(function() {
    $(".mytr td").click(function() {
         $(this).css("background-color", "#000000");
    });
});

谢谢大家......问题是在masterpage中我before query-1.3.2-vsdoc.js before加载了j query-1.3.2.min.js ,这样它就不能query-1.3.2-vsdoc.js再次query-1.3.2-vsdoc.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM