【问题标题】:jQuery table row highlight onClick and restore onChange alternating css classesjQuery 表格行突出显示 onClick 并恢复 onChange 交替 css 类
【发布时间】:2023-03-27 20:19:01
【问题描述】:

第一篇文章和 jQuery 新手。苦苦寻找,但没有找到完全相关的帖子。

在表格中有交替的行颜色;一类 NULL/空一类 class=alt。 jQuery 脚本部分用于在单击时突出显示该行。

问题是:如何使用 removeClass 然后恢复(addClass/toggleClass)在 onChange 事件中删除的特定类?任何帮助表示赞赏。谢了。 (见下面的小提琴)

HTML

    <table id="fooBar" border="0">
    <tr>
        <th>Text</th>
        <th>Text</th>
        <th>Text</th>
        <th></th>
        <th></th>
    </tr>
    <tr class="">
        <td>Text</td>
        <td></td>
        <td></td>
        <td></td>
        <td>image</td>
        <td>image</td>
    </tr>
    <tr class="alt">
        <td>Text</td>
        <td></td>
        <td></td>
        <td></td>
        <td>image</td>
        <td>image</td>
    </tr>
    <tr class="">
        <td>Text</td>
        <td></td>
        <td></td>
        <td></td>
        <td>image</td>
        <td>image</td>
    </tr>
    <tr class="alt">
        <td>Text</td>
        <td></td>
        <td></td>
        <td></td>
        <td>image</td>
        <td>image</td>
    </tr>
</table>

CSS

table {
    width:100%;
    border-collapse:collapse;
    table-layout:auto;
    vertical-align:top;
    margin-bottom:15px;
    border:1px solid #999999;
}

th {
    font: bold 11px"Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
    color: #F2EDEB;
    border-right: 1px solid #C1DAD7;
    border-bottom: 1px solid #C1DAD7;
    border-top: 1px solid #C1DAD7;
    letter-spacing: 2px;
    text-transform: uppercase;
    text-align: left;
    padding: 6px 6px 6px 12px;
    background: #522D25 url(images/bg_header.jpg) no-repeat;
}

tr {
    background: #fff;
    color: #261F1D;
}

tr:hover, tr.alt:hover {
    color: #261F1D;
    background-color: #E5C37E;
}

.highlighted {
    color: #261F1D;
    background-color: #E5C37E;
}

tr.alt {
    background: #F5FAFA;
    color: #B4AA9D;
}

td {
    border-right: 1px solid #C1DAD7;
    border-bottom: 1px solid #C1DAD7;
    padding: 6px 6px 6px 12px;
}

jQuery

$(document).ready(function () {
    $("tr").click(function () {
        $(this).closest("tr").siblings().removeClass("highlighted");
        $(this).toggleClass("highlighted");
    })
});
// Note: currently only works for row without '.alt' css class assigned (ie. empty)
// question is how to replace a class and then RESTORE the class onChange

这里是 jsFiddle:http://jsfiddle.net/codeChaos1988/kh4VY/1/

此外,任何关于实际学习 javascript/jQuery 编程的最佳教程网站的指针也将不胜感激。

提前感谢您的帮助。

【问题讨论】:

    标签: php jquery html css


    【解决方案1】:

    为了让您不必担心删除/恢复原始类,请尝试将您的 css 更改为:

    .highlighted {
        color: #261F1D !important;
        background-color: #E5C37E !important;
    }
    

    对于 JS:

    $('table').on('click', 'tr', function () {
        var state = $(this).hasClass('highlighted');
        $('.highlighted').removeClass('highlighted');
        if ( ! state) { $(this).addClass('highlighted'); }
    });
    

    【讨论】:

    • 谢谢你。工作完美!希望其他用户可以使用这篇文章。干杯!!!
    【解决方案2】:

    在数据表初始化部分添加了这个:

    "createdRow": function( row, data, index ) {
                if (tableInitialized){
                    flashRow( row );
                }
    

    成功了!

    【讨论】:

      猜你喜欢
      • 2016-11-08
      • 2010-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-01
      • 2016-07-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多