html - 活动复选框更改除了单元格之外的表格背景颜色
问题描述
当复选框处于非活动状态时,表格不会更改,一旦单击复选框,所有单元格的背景颜色都会变为黄色,除了中间的变为红色。 这就是我需要达到的结果
label {
-webkit-appearance: button;
-moz-appearance: button;
appearance: button;
display: inline-block;
cursor: pointer;
margin:5px;
}
#checkbox_id:checked + table {
background-color: yellow;
}
/*Table styles*/
.tg {border-collapse:collapse;border-spacing:0;}
.tg td{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;
overflow:hidden;padding:10px 5px;word-break:normal; width: 50px; height: 50px;}
.tg th{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;
font-weight:normal;overflow:hidden;padding:10px 5px;word-break:normal; width: 50px; height: 50px;}
.tg .tg-0lax{text-align:left;vertical-align:top}
<!DOCTYPE html>
<html lang="it" dir="ltr">
<head>
<meta charset="utf-8">
<title>Check6</title>
</head>
<body>
<label for="checkbox_id">CLICCA</label>
<input type="checkbox" id="checkbox_id">
<table class="tg">
<thead>
<tr>
<th class="tg-0lax"></th>
<th class="tg-0lax"></th>
<th class="tg-0lax"></th>
</tr>
</thead>
<tbody>
<tr>
<td class="tg-0lax"></td>
<td id="red"></td>
<td class="tg-0lax"></td>
</tr>
<tr>
<td class="tg-0lax"></td>
<td class="tg-0lax"></td>
<td class="tg-0lax"></td>
</tr>
</tbody>
</table>
</body>
</html>
已经感谢您的帮助。
解决方案
将以下代码添加到您的 CSS 中:
#checkbox_id:checked+table #red {
background-color: red;
}
这是一个代码片段:
label {
-webkit-appearance: button;
-moz-appearance: button;
appearance: button;
display: inline-block;
cursor: pointer;
margin: 5px;
}
#checkbox_id:checked+table {
background-color: yellow;
}
#checkbox_id:checked+table #red {
background-color: red;
}
/*Table styles*/
.tg {
border-collapse: collapse;
border-spacing: 0;
}
.tg td {
border-color: black;
border-style: solid;
border-width: 1px;
font-family: Arial, sans-serif;
font-size: 14px;
overflow: hidden;
padding: 10px 5px;
word-break: normal;
width: 50px;
height: 50px;
}
.tg th {
border-color: black;
border-style: solid;
border-width: 1px;
font-family: Arial, sans-serif;
font-size: 14px;
font-weight: normal;
overflow: hidden;
padding: 10px 5px;
word-break: normal;
width: 50px;
height: 50px;
}
.tg .tg-0lax {
text-align: left;
vertical-align: top
}
<!DOCTYPE html>
<html lang="it" dir="ltr">
<head>
<meta charset="utf-8">
<title>Check6</title>
</head>
<body>
<label for="checkbox_id">CLICCA</label>
<input type="checkbox" id="checkbox_id">
<table class="tg">
<thead>
<tr>
<th class="tg-0lax"></th>
<th class="tg-0lax"></th>
<th class="tg-0lax"></th>
</tr>
</thead>
<tbody>
<tr>
<td class="tg-0lax"></td>
<td id="red"></td>
<td class="tg-0lax"></td>
</tr>
<tr>
<td class="tg-0lax"></td>
<td class="tg-0lax"></td>
<td class="tg-0lax"></td>
</tr>
</tbody>
</table>
</body>
</html>
推荐阅读
- android - 从 dart/flutter 在 POS USB 打印机上打印
- ag-grid - 从选项列表拖放到 agggrid 表中
- java - 知道 Web 服务器上文件夹中的文件
- java - 在 Maven 程序中将程序数据写入 txt 文件时遇到问题
- google-bigquery - 嵌套(数组)数据的 BigTable 设计
- python - 如何在 Pytorch 中打印调整学习率?
- jmeter - 为什么正文不包含在 jmeter 请求中?
- flutter - 如何在flutter中使用动态索引调用类内的get方法?
- html - 将静态半圆与带有 html/css 的相当大的矩形相结合
- cucumber - 如何将场景大纲标记为已知缺陷?