[英]Change color row Datatable selected when click on button Javascript
I need to change the color of a row in my Datatable when I click on the button 'APPLY'单击“应用”按钮时,我需要更改数据表中一行的颜色
Here is my HTML code for the rows :这是我的行的 HTML 代码:
<tr role="row" class="odd"></tr>
<tr role="row" class="even selected"></tr>
<tr role="row" class="odd"></tr>
<tr role="row" class="even"></tr>
In my inspect tab I can change the background color of the row like this :在我的检查选项卡中,我可以像这样更改行的背景颜色:
table.dataTable tbody tr.selected {
background-color: orange;
}
How can I code this in Javascript?如何在 Javascript 中编码? I tried this code but I obtained an error :
我试过这段代码,但我得到了一个错误:
$("#btnApply").click(function() {
var zz = document.getElementsByClassName("selected");
zz.style.backgroundColor = "orange";
});
The error :错误 :
Uncaught TypeError: Cannot set property 'backgroundColor' of undefined
未捕获的类型错误:无法设置未定义的属性“backgroundColor”
As you use jQuery in the first line, Y not to continue using it like that:当您在第一行中使用 jQuery 时,Y 不要像这样继续使用它:
$("#btnApply").click(function() { $(".selected").css('background-color', 'green'); });
table tr.selected { background-color: orange; }
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script> <table> <tr role="row" class="odd"><td>aa</td></tr> <tr role="row" class="even selected"><td>aa</td></tr> <tr role="row" class="odd"><td>aa</td></tr> <tr role="row" class="even"><td>aa</td></tr> </table> <input type="button" id="btnApply" value="click">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.