javascript - 在 JQuery 中使用自定义数据属性将 css 应用于 tr

标签 javascript jquery html css

我有一个表,每个 tr 元素都有一个自定义数据属性,我想对某些值应用样式。

在 HTML 中:

<table id="notify" class="t_notify">
<tr data-notify="101">
 <td>data</td>
 <td>data</td>
</tr>
<tr data-notify="102">
 <td>data</td>
 <td>data</td>
</tr>
<!-- and goes -->
</table>

使用 CSS:

.t_notify>tbody>tr{background:#BBB;}
.t_notify>tbody>tr:hover{background:transparent;}

我在 Jquery 中尝试在单击某个 tr 元素时执行此操作,例如:

$("#notify > tbody > tr").attr("[data-notify='101']").css({"background":"#CCC"});

但这行不通。我需要一些帮助。

最佳答案

好的方法

您的查询必须在选择器中,如下所示:

$("#notify > tbody > tr[data-notify='101']").css({"background":"#CCC"});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="notify" class="t_notify">
<tr data-notify="101">
 <td>data</td>
 <td>data</td>
</tr>
<tr data-notify="102">
 <td>data</td>
 <td>data</td>
</tr>
<!-- and goes -->
</table>

属性()

attr() 函数返回属性的值或设置它。就您而言,您两者都不想要。希望对您有所帮助。

关于javascript - 在 JQuery 中使用自定义数据属性将 css 应用于 tr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46003481/

相关文章:

javascript - 使用 jquery 检查提交时 FORM POST 的值

javascript - 类型错误 : Object of type 'Category' (model) is not JSON serializable

javascript - 如何有效地限制鼠标悬停事件时执行的 AJAX 请求?

javascript - 弹出框内的旋转木马 - Bootstrap

javascript - 在 firefox 操作系统中使用 mediarecorder api 和 html5 音频标签无法进行音频捕获和播放

javascript - Angular - 如何将多个 FormGroups 插入 FormArray?

javascript - 阻止 url 直接访问 index.php,除非用户单击另一个特定 html 文件(例如 index.html)上的链接

javascript - 短时间后更改回图像源

html - bootstrap 3 中的表单对齐

javascript - 为什么没有适用于 IE9 等旧版浏览器的 flexbox polyfill?