英文:
How do I only highlight rows of a table on hover except the last column?
问题
问题类似于此帖子:https://stackoverflow.com/questions/41770244/html-table-how-highlight-row-on-hover-except-first-column。然而,这里的回复不是我要的效果。我希望当我悬停在任何一个列上时,前两列都会高亮显示,而当我悬停在第三列上时,不会有任何高亮显示。
英文:
The problem I am facing is similar to this post: https://stackoverflow.com/questions/41770244/html-table-how-highlight-row-on-hover-except-first-column. However the replies here are not the effect that I am looking for. I am looking for both the first 2 columns to light up when I hover over either one, and for the 3rd column to not light up anything at all when I hover over it.
答案1
得分: 1
请不要使用任何 [tag:javascript] 或 [tag:jquery],只需两个 [tag:css] 规则即可。
选择所有 tr:hover
,然后通过使用 td:not(:last-of-type)
来排除最后一列。
然后禁用最后一列上的所有 pointer-events
,以防止触发 hover 事件。
table>tbody>tr:hover td:not(:last-of-type) {
background-color: red;
}
table>tbody>tr>td:last-of-type {
pointer-events: none;
}
英文:
Please don't use any [tag:javascript] nor [tag:jquery] for this, two [tag:css] rule's are enough.
Select all the tr:hover
, and then exclude the last col by using td:not(:last-of-type)
.
Then disable all pointer-events
on the last column to disable that triggering the hover.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
table>tbody>tr:hover td:not(:last-of-type) {
background-color: red;
}
table>tbody>tr>td:last-of-type {
pointer-events: none;
}
<!-- language: lang-html -->
<table border cellpadding="5">
<tbody>
<tr>
<td>F</td>
<td>o</td>
<td>o</td>
<td>B</td>
<td>a</td>
<td>r</td>
</tr>
<tr>
<td>F</td>
<td>o</td>
<td>o</td>
<td>B</td>
<td>a</td>
<td>r</td>
</tr>
</tbody>
</table>
<!-- end snippet -->
答案2
得分: -1
如果您需要在第三列中触发鼠标事件,这将无法在没有JavaScript的情况下完成。
问题是第三列应该被禁用。可以使用pointer-events:none
来实现。但如果我们需要在第三列中触发鼠标事件,以下是解决方案:
您可以使用nth-child
CSS伪类和一些JavaScript:
https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-child
'mousemove mouseleave'.split(' ').forEach(name =>
$table.addEventListener(name, syncHover)
);
function syncHover(e) {
$table.querySelector('tr.hover')?.classList.remove('hover');
const $td = e.target.closest('td');
$td && [...$td.parentElement.children].indexOf($td) < 2 &&
$td.parentElement.classList.add('hover');
}
table tr.hover>td:nth-child(-n + 2) {
background: lightgray;
cursor: pointer;
}
table td {
padding: 5px 10px;
}
table {
border-collapse: collapse;
background: #eee;
}
<table id="$table">
<tr>
<td>col1</td>
<td>col2</td>
<td>col3</td>
</tr>
<tr>
<td>col1</td>
<td>col2</td>
<td>col3</td>
</tr>
<tr>
<td>col1</td>
<td>col2</td>
<td>col3</td>
</tr>
<tr>
<td>col1</td>
<td>col2</td>
<td>col3</td>
</tr>
</table>
英文:
If you need mouse events in the 3rd column this cannot be done without JS.
The problem that the 3rd column should be disabled. It could be done with pointer-events:none
. But if we need mouse events in the 3rd columns here's solution:
You could use the nth-child
CSS pseudo class and some Javascript:
https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-child
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
'mousemove mouseleave'.split(' ').forEach(name =>
$table.addEventListener(name, syncHover)
);
function syncHover(e) {
$table.querySelector('tr.hover')?.classList.remove('hover');
const $td = e.target.closest('td');
$td && [...$td.parentElement.children].indexOf($td) < 2 &&
$td.parentElement.classList.add('hover');
}
<!-- language: lang-css -->
table tr.hover>td:nth-child(-n + 2) {
background: lightgray;
cursor: pointer;
}
table td {
padding: 5px 10px;
}
table {
border-collapse: collapse;
background: #eee;
}
<!-- language: lang-html -->
<table id="$table">
<tr>
<td>col1</td>
<td>col2</td>
<td>col3</td>
</tr>
<tr>
<td>col1</td>
<td>col2</td>
<td>col3</td>
</tr>
<tr>
<td>col1</td>
<td>col2</td>
<td>col3</td>
</tr>
<tr>
<td>col1</td>
<td>col2</td>
<td>col3</td>
</tr>
</table>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论