Change tr background-color

后端 未结 7 1416
刺人心
刺人心 2021-02-04 04:36

I have something like this:

\' onclick=\"SetBackgroundColor(this)\" style=\"background-color:Yellow\">

Whe

相关标签:
7条回答
  • 2021-02-04 05:03

    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");
        });
    });
    
    0 讨论(0)
  • 2021-02-04 05:04

    In jQuery you do not have to use the onclick attribute to assign an event handler. Lets say you add a class called mytr to each tr that you want to affect. 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.

    0 讨论(0)
  • 2021-02-04 05:04

    Thank you all..the problem was that in the masterpage i was loading the jquery-1.3.2.min.js before query-1.3.2-vsdoc.js and that's way it wasn't working..thanks again

    0 讨论(0)
  • 2021-02-04 05:11
     $('#RowID').children('td, th').css('background-color','yellow');
    
    0 讨论(0)
  • 2021-02-04 05:14

    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/

    0 讨论(0)
  • 2021-02-04 05:15

    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');
    });
    
    0 讨论(0)
提交回复
热议问题