颜色表的行,使用JavaScript和CSS单击单击
我有一张桌子,想在使用JavaScript点击时对一行着色。
这是我的代码,表中有三行,JavaScript中的两个功能和一个CSS文件:
function changeColor() {
var td = $("#myTable" + " td");
$.each(td, function(i) {
if ($(td[i]).html() === 'Cloture') {
$(td[i]).addClass("green");
} else if ($(td[i]).html() === 'A faire') {
$(td[i]).addClass("red");
} else if ($(td[i]).html() === 'En cours') {
$(td[i]).addClass("orange");
}
else {
$(td[i]).addClass("white");
}
});
}
changeColor();
function selectRow() {
var table = document.getElementById("myTable");
[...table.rows].forEach(el => {
el.addEventListener('click', () => {
el.classList.toggle('selected-row');
});
});
}
selectRow();
td{
text-align: center;
color: white;
background: #FFFFFF;
padding: 10px;
}
tr:hover {background-color: #FFE56C;}
td:last-child {
border-right: 1px;
}
.green {
background-color: #239e58 !important;
}
.red {
background-color: #ff4e33 !important;
}
.orange {
background-color: #ffd633 !important;
}
.white {
background-color: white !important;
}
.table-row {
cursor: pointer;
}
.selected-row {
background-color: #FFD200 !important;
color: white;
}
<html>
<title>Actions EAVP</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/datepicker/1.0.10/datepicker.min.js" integrity="sha512-RCgrAvvoLpP7KVgTkTctrUdv7C6t7Un3p1iaoPr1++3pybCyCsCZZN7QEHMZTcJTmcJ7jzexTO+eFpHk4OCFAg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css">
<link rel="stylesheet" type="text/css" href="/static/polls/style.css" />
<div class="container">
<body style="background-color: white";>
<section class="content">
<div>
</div>
<br>
<h3 style="color: black;">Liste des actions</h3>
<div style="overflow-x:auto;">
<table class="list_homepage" id="myTable">
<th style="color: white; background-color: #03234B; padding: 5px; border: 1px solid white; text-align: center"></th>
<th style="color: white; background-color: #03234B; padding: 5px; border: 1px solid white; text-align: center">id</th>
<th style="color: white; background-color: #03234B; padding: 5px; border: 1px solid white; text-align: center">titre</th>
<th style="color: white; background-color: #03234B; padding: 5px; border: 1px solid white; text-align: center">date_rendu</th>
<th style="color: white; background-color: #03234B; padding: 5px; border: 1px solid white; text-align: center">statut</th>
<th style="color: white; background-color: #03234B; padding: 5px; border: 1px solid white; text-align: center">date_insertion</th>
<th style="color: white; background-color: #03234B; padding: 5px; border: 1px solid white; text-align: center">zone_emettrice</th>
<th style="color: white; background-color: #03234B; padding: 5px; border: 1px solid white; text-align: center">zone_receptrice</th>
<th style="color: white; background-color: #03234B; padding: 5px; border: 1px solid white; text-align: center">owner</th>
<tr class="table-row">
<td style="border-bottom: 1px solid black;">
<a style="color: #ff0000; text-align: center;" href="/action/1/delete/"><i class="bi bi-trash3" style="font-size: 30px;"></i></a>
<a style="color: #3CB4E6; text-align: center;" href="/action/1/update/"><i class="bi bi-pencil-square" style="font-size: 30px;"></i></a>
<br>
<a style="; text-align: center;" href="/action/1/">Detail</a></td>
<td style="color: black; text-align: center; border-bottom: 1px solid black;">1</td>
<td style="color: black; text-align: center; border-bottom: 1px solid black;">Data</td>
<td style="color: black; text-align: center; border-bottom: 1px solid black;">2022-05-20 22:00:00+00:00</td>
<td style="color: black; text-align: center; border-bottom: 1px solid black;">A faire</td>
<td style="color: black; text-align: center; border-bottom: 1px solid black;">2022-05-06 13:17:07+00:00</td>
<td style="color: black; text-align: center; border-bottom: 1px solid black;">Data</td>
<td style="color: black; text-align: center; border-bottom: 1px solid black;">Data</td>
<td style="color: black; text-align: center; border-bottom: 1px solid black;">Data</td>
</tr>
<tr class="table-row">
<td style="border-bottom: 1px solid black;">
<a style="color: #ff0000; text-align: center;" href="/action/2/delete/"><i class="bi bi-trash3" style="font-size: 30px;"></i></a>
<a style="color: #3CB4E6; text-align: center;" href="/action/2/update/"><i class="bi bi-pencil-square" style="font-size: 30px;"></i></a>
<br>
<a style="; text-align: center;" href="/action/2/">Detail</a></td>
<td style="color: black; text-align: center; border-bottom: 1px solid black;">2</td>
<td style="color: black; text-align: center; border-bottom: 1px solid black;">Data</td>
<td style="color: black; text-align: center; border-bottom: 1px solid black;">2022-05-24 22:00:00+00:00</td>
<td style="color: black; text-align: center; border-bottom: 1px solid black;">Cloture</td>
<td style="color: black; text-align: center; border-bottom: 1px solid black;">2022-05-06 13:18:34+00:00</td>
<td style="color: black; text-align: center; border-bottom: 1px solid black;">Data</td>
<td style="color: black; text-align: center; border-bottom: 1px solid black;">Data</td>
<td style="color: black; text-align: center; border-bottom: 1px solid black;">Data</td>
</tr>
<tr class="table-row">
<td style="border-bottom: 1px solid black;">
<a style="color: #ff0000; text-align: center;" href="/action/5/delete/"><i class="bi bi-trash3" style="font-size: 30px;"></i></a>
<a style="color: #3CB4E6; text-align: center;" href="/action/5/update/"><i class="bi bi-pencil-square" style="font-size: 30px;"></i></a>
<br>
<a style="; text-align: center;" href="/action/5/">Detail</a></td>
<td style="color: black; text-align: center; border-bottom: 1px solid black;">5</td>
<td style="color: black; text-align: center; border-bottom: 1px solid black;">Data</td>
<td style="color: black; text-align: center; border-bottom: 1px solid black;">2022-05-23 22:00:00+00:00</td>
<td style="color: black; text-align: center; border-bottom: 1px solid black;">En cours</td>
<td style="color: black; text-align: center; border-bottom: 1px solid black;">2022-05-06 14:24:29+00:00</td>
<td style="color: black; text-align: center; border-bottom: 1px solid black;">Data</td>
<td style="color: black; text-align: center; border-bottom: 1px solid black;">Data</td>
<td style="color: black; text-align: center; border-bottom: 1px solid black;">Data</td>
</tr>
</table>
</div>
如您所见,我还有另一个可正常工作的JavaScript函数,但是点击上色的功能无法正常工作。
如果有人可以解决我的问题,那就太好了!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用几行,例如:
您可以将
table.Rows
与dread ... 一起创建所有行的数组,然后只需使用addeventListener
与foreach
代码>用于分配切换单击类。参考:
使用您的新snippet:
您有两个问题:
You can reach your object with few lines like:
You can use
table.rows
with spread...
for create an array of all rows then just useaddEventListener
withforEach
for assign a toggle click class.Reference:
with your new snippet:
You have two problem: