javascript - 更改数据表中单击的行的颜色
问题描述
如何更改数据表单击行的颜色?
我有我的数据表和一个 JavaScript 来对点击的行的数据进行一些操作:
var table = $('#myTable').DataTable();
$('#myTable tbody').on('click', 'tr', function () {
var data = table.row( this ).data();
// below some operations with the data
// How can I set the row color as red?
} );
谢谢
解决方案
在这种情况下,我不确定 DataTable 的相关性,但您可以使用 jQuery 来做到这一点。
示例:https ://codepen.io/alexpetergill/pen/ccd10f785e6b6d9e2c2b2503b219eab4
var table = $('#myTable').DataTable();
$('#myTable').on('click', 'tr', function () {
var data = table.row( this ).data();
// below some operations with the data
// How can I set the row color as red?
$(this).addClass('highlight').siblings().removeClass('highlight');
});
如果您想在分页后删除突出显示,那么我建议您使用 DataTables 自定义事件之一...
$('#myTable').on('draw.dt', function () {
$(this).find('.highlight').removeClass('highlight');
});
推荐阅读
- android - 如何在kotlin中创建对象数组并按索引插入值
- asp.net-core - 我在尝试使用 JWT-ASP.NETCORE 3.1 时遇到 ıdx10653 错误
- c# - 创建和合并 GitHub 拉取请求 API C#
- java - 在没有 setter/getter 的情况下切换其他类中的变量
- docker - 将 gitlab ssh 公钥添加到公司防火墙后面的 dockerfile 中的已知主机(无端口 22)
- wordpress-theming - 已删除儿童主题内容
- reactjs - 如何将自定义道具传递给 SwiperSlide 并获取它的 onSlideChange 函数?
- apache-spark - 在火花中加载未知分区大小时防止OOO
- angular - Angular - 基于文本框中未输入的值禁用另一个控件
- javascript - useSWRInfinite - getKey 函数总是将 pageIndex 获取为 1