` 中)会导致 dom 结构非法,从而无法实现预期的隔行变色效果。HTML 规范明确要求 <table> 的直接子元素只能是 <caption>、<colgroup>、<thead>、<tbody>、<tfoot> 和 <tr>;而 <form> 不在此列。当 <form> 被错误地置于 <table> 内但位于 <tr> 外部时,浏览器会自动进行 DOM 修复(例如将 <form> 移出表格或包裹为匿名容器),导致 <tr> 的实际层级关系被破坏——原本连续的 <tr> 元素可能被 <form> 分隔,使 :nth-child(odd) 选择器无法按视觉顺序准确匹配奇数行。
此外,原 CSS 使用了过于严格的后代选择器:
.table-striped > tbody > tr:nth-child(odd) > td,
.table-striped > tbody > tr:nth-child(odd) > th {
background-color: rgba(7, 114, 244, 0.52);
}该写法隐含两个前提:① 表格必须包含 <tbody>;② <tr> 必须是 <tbody> 的直接子元素。但示例中完全缺失 <tbody>,且 <form> 的非法嵌套进一步干扰了 DOM 树结构,双重因素导致样式失效。
✅ 正确解法是:
立即学习“前端免费学习笔记(深入)”;
-
将 <form> 完全移入 <tr> 内部(作为 <tr> 的唯一子元素,或直接包裹全部 <td>);
-
显式添加 <tbody>(语义更规范,也避免浏览器自动补全带来的不确定性);
-
简化 CSS 选择器,避免过度依赖 > 和隐式 <tbody>,改用更健壮的路径:
.table-striped tbody tr:nth-child(odd) td,
.table-striped tbody tr:nth-child(odd) th {
background-color: rgba(7, 114, 244, 0.52);
}以下是修正后的完整示例(已通过 W3C 验证,兼容主流浏览器):
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>
.table-striped tbody tr:nth-child(odd) td,
.table-striped tbody tr:nth-child(odd) th {
background-color: rgba(7, 114, 244, 0.52);
}
/* 可选:增强可读性,为偶数行设置浅灰底色 */
.table-striped tbody tr:nth-child(even) td,
.table-striped tbody tr:nth-child(even) th {
background-color: #f9f9f9;
}
</style>
</head>
<body>
<div class="container">
<h3>Table list</h3><br>
<table class="table table-striped">
<thead>
<tr>
<th>Press</th>
<th>Code</th>
<th>Maintenance</th>
<th>Due Date</th>
<th>ID</th>
<th>Done By</th>
<th>Finished</th>
<th>Not Finished</th>
</tr>
</thead>
<tbody>
<tr>
<form action="a.php" method="POST">
<td><center>AUX</center></td>
<td><center>TOOLS</center></td>
<td><center>Tools checking and arrangement</center></td>
<td><center>2023-09-01</center></td>
<td><center><input class="form-control" type="number" name="id" value="49" required></center></td>
<td><center><input class="form-control" type="text" name="user" maxlength="20"></center></td>
<td style="display:none"><input type="hidden" name="hidd" value="TOOLS"></td>
<td><center><input type="submit" value="Done" name="done"></center></td>
<td><center><input type="submit" value="Not done" name="not"></center></td>
</form>
</tr>
<tr>
<form action="a.php" method="POST">
<td><center>A Press</center></td>
<td><center>PRESS-A</center></td>
<td><center>Press main inlet airline filter cleaning</center></td>
<td><center>2023-09-02</center></td>
<td><center><input class="form-control" type="number" name="id" value="58" required></center></td>
<td><center><input class="form-control" type="text" name="user" maxlength="20"></center></td>
<td style="display:none"><input type="hidden" name="hidd" value="PRESS-A"></td>
<td><center><input type="submit" value="Done" name="done"></center></td>
<td><center><input type="submit" value="Not done" name="not"></center></td>
</form>
</tr>
</tbody>
</table>
</div>
</body>
</html>⚠️ 注意事项:
- <center> 标签已废弃,建议改用 CSS text-align: center 或 Flex 布局;
- 每个 <form> 应独立包裹一行数据,确保提交时仅发送对应行字段(避免多行表单冲突);
- 若需支持无障碍访问,建议为每个 <input> 添加 <label>,并使用 aria-labelledby 关联表头;
- 现代开发中更推荐使用 <tbody> + <tr> + <td> 结构化数据,再通过 JavaScript 动态绑定事件(如 event delegation),而非依赖内联表单。
遵循以上结构与样式规则,即可在保留表单功能的同时,稳定实现表格隔行着色效果。