html - 在最后一次出现类之后和使用 CSS 的特定类之前选择元素
问题描述
我试图在不使用 JavaScript 循环的情况下解决问题。我想将 css 规则应用于表格单元格。我在开始单元格之前很难应用规则。
我发现这个问题Select all elements before element with class? 我很难实现这种技术,因为我的代码需要更复杂的检查。尤其是考虑在开始前处理两个预订时。
如果我能在开始前找到最后一个预订,我可以使用该问题中的技术。
有谁知道如何在没有 javascript 的情况下解决这个问题?
table {
margin: auto;
width: 100%;
border: thick groove black;
border-radius: 5px;
text-align: center;
}
td {
border: thin groove gray;
width: 10vw;
height: 5vh;
}
td:hover {
cursor: pointer;
}
td.start {
background-color: green;
}
td.booked {
background-color: orange;
cursor: not-allowed;
}
td.start ~ td:not(.booked) {
background-color: lightblue;
}
td.start ~ td ~ td.booked:hover {
cursor: not-allowed;
}
td.start ~ td ~ td.booked ~ td {
background-color: red;
}
td.start ~ td ~ td.booked ~ td:hover {
cursor: not-allowed;
}
<br />
<table>
<thead>
<tr>
<td>Invalid</td>
<td>Invalid</td>
<td>Booked</td>
<td>OK</td>
<td>OK</td>
<td>Start</td>
<td>OK</td>
<td>OK</td>
<td>Booked</td>
<td>Invalid</td>
<td>Invalid</td>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td class="booked"></td>
<td></td>
<td></td>
<td class="start"></td>
<td></td>
<td></td>
<td class="booked"></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<p
style="text-align:center; border: thin groove gray; border-radius:5px; margin: 1.25rem 4vw;
padding: 1.25rem;
"
>
Relative to th start cell, cells after a booking are not allowed. Cells before a booking are allowed.
<br />
<br />
We can select the cells after the start cell with: <br /><br />
<code
style="background-color:lightgray; padding:.75rem;
border-radius: 5px
"
>td.start ~ td:not(.booked) { background-color: lightblue; }</code
>
<br />
<br />
We can select the cells after the booked cell after the start cell with:
<br /><br />
<code
style="background-color:lightgray; padding:.75rem;
border-radius: 5px
"
>td.start ~ td ~ td.booked ~ td { background-color: red; }</code
>
<br />
<br />
<br />
<strong>
Q. How can we select the cells before the start cell and apply the rules ?</strong
>
<br />
</p>
解决方案
在这种情况下,我认为无法通过链接问题的下一个兄弟组合器和通用选择器组合来实现。使用 JavaScript 或 apply class
,为data-*
每个td
元素添加属性。
推荐阅读
- jsp - 我如何将选项从下拉列表传递到 foreach 中的数据 bean 方法
- python - 区间比较中的未知变量不会引发 NameError
- angular - Deixar item pre selecionado no P-TableRadioButton
- javascript - 我的汉堡包下拉菜单出现在移动设备上的“工作原理”页面中的滑块后面,并且分页与文本混合在一起
- forms - Vue表单空字段
- java - 使用 Spark 和 Java8 从 Dataframe 中获取多列的不同值的计数
- javascript - 在异步方法中循环对象
- jmeter - 无法通过 JMeter 发送 HTTP2 请求
- classification - 方差很大的一维数据的最佳判别方法
- hadoop - 如何使用 Hive 检查 Druid 数据源