如何为Twitter Bootstrap切换表格行背景颜色?

use*_*398 5 css jquery twitter-bootstrap

参考我之前的帖子:

我的HTML

<table class="table table-bordered table-condensed table-striped">
    <thead>
        <tr>
            <th>Col 1</th>
            <th>Col 2</th>
            <th>Col 3</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td><strong>Data 1</strong></td>
            <td>Data 2</td>
            <td>Data 3</td>
        </tr>
        <tr>
            <td><strong>Data 1</strong></td>
            <td>Data 2</td>
            <td>Data 3</td>
        </tr>
        <tr>
            <td><strong>Data 1</strong></td>
            <td>Data 2</td>
            <td>Data 3</td>
        </tr>
    </tbody>
</table>
Run Code Online (Sandbox Code Playgroud)

jQuery的

$(document).ready(function(){
    $('table.table-striped tr').on('click', function () {
        $(this).find('td').css('background-color', '#ff0000');

         // toggleClass doesn't seem to work

     });
 });
Run Code Online (Sandbox Code Playgroud)

我正试图通过我的点击事件切换关闭的行颜色.是否可以使用用户定义的css选择器?

The*_*pha 6

你可以试试这个

CSS

.bg{background-color:#ff0000 !important;}?
Run Code Online (Sandbox Code Playgroud)

JS

$(document).ready(function(){
    $('table.table-striped tbody tr').on('click', function () {
        $(this).closest('table').find('td').removeClass('bg');
        $(this).find('td').addClass('bg');
    });
});?
Run Code Online (Sandbox Code Playgroud)

演示.

更新:切换

$(document).ready(function(){
    $('table.table-striped tbody tr').on('click', function () {
        $(this).find('td').toggleClass('bg');
    });
});?
Run Code Online (Sandbox Code Playgroud)

演示.