Highlighting the clicked row of a striped HTML table

后端 未结 5 1788
夕颜
夕颜 2020-12-12 21:54

Here\'s an example of my problem on jsFiddle.

I have a table with striped rows imposed by using tr:nth-child(odd) in the CSS, as is done in Twitter Boot

5条回答
  •  盖世英雄少女心
    2020-12-12 22:36

    http://jsfiddle.net/iambriansreed/xu2AH/9/

    .table-striped class

    .table-striped tbody tr.highlight td { background-color: red; }
    

    ... and cleaner jQuery:

    $('#mytable tbody tr').live('click', function(event) {
        $(this).addClass('highlight').siblings().removeClass('highlight');
    });​
    

    Update: .live() has since been deprecated. Use .on().

    $('#mytable').on('click', 'tbody tr', function(event) {
        $(this).addClass('highlight').siblings().removeClass('highlight');
    });​
    

    Fixed: http://jsfiddle.net/iambriansreed/xu2AH/127/

提交回复
热议问题