[英]Hover td escapes background-color change from hover tr
I have a general class that I use for table rows which changes the background-color
of it's td 's when they are hovered. 我有一个用于表行的通用类,当它们悬停时会改变它的td的
background-color
。 I'm using it all over my application. 我在我的应用程序中使用它。
.enty-table {
&:hover > td {
background-color: lightblue;
}
}
<table class="table table-bordered">
<thead>
<tr>
<th></th>
<th>Name</th>
<th>Min Length</th>
<th>Max Length</th>
<th>Min Value</th>
<th>Max Value</th>
<th>Regular Expr</th>
<th>Default Value</th>
</tr>
</thead>
<tbody>
<tr class="enty-table">
<!-- I want the hover on the folowing td to escape the hover effect added with enty-table class on tr -->
<td class="columnCategory">Business Fields</td>
<td><strong>new column1</strong></td>
<td><span></span></td>
<td><span></span></td>
<td><span></span></td>
<td><span></span></td>
<td><span></span></td>
<td><span></span></td>
</tr>
<tr class="enty-table">
<td><strong>new column2</strong></td>
<td><span></span></td>
<td><span></span></td>
<td><span></span></td>
<td><span></span></td>
<td><span></span></td>
<td><span></span></td>
</tr>
</tbody>
</table>
Now, I have a special table (2 dimensional) where in the first column I have td 's with rowspan. 现在,我有一个特殊的表(2维),在第一列我有td的行。
Eventually I want to get rid of background-color change when I hover the rowspan td : 最后,当我悬停rowspan td时,我想摆脱背景颜色的变化:
When I hover on the Business Fields
td the hover effect is applied for the new column1
row, but when I hover on the second row it is not applied on the td with rowspan . 当我将鼠标悬停在该
Business Fields
TD悬停效果的方法适合于new column1
行,但是当我将鼠标悬停在第二行,它不会应用与行跨度的TD。 I'm want to fix that by removing the hover action from the first td . 我想通过从第一个td中删除悬停动作来解决这个问题。
How can I escape the hover effect on the rowspan td , but keep it for the table rows (the individual subrows - new column1
, new column2
)? 如何在rowspan td上转义悬停效果,但保留表行(各个子行 -
new column1
, new column2
)?
Can it only be done from CSS? 它只能通过CSS完成吗?
You could use CSS :not()
pseudo-class to ignore the <td>
has rowspan
attribute, then use CSS general sibling selectors to reset the background color of table cells, as follows: 您可以使用CSS
:not()
伪类来忽略<td>
具有rowspan
属性,然后使用CSS通用兄弟选择器重置表格单元格的背景颜色,如下所示:
.enty-table {
&:hover > td:not([rowspan]) {
background-color: lightblue;
}
& > td[rowspan]:hover ~ td {
background-color: #fff; /* Reset the background of next cells */
}
}
JSBin Demo . JSBin演示 。
If using CSS :not()
is not an option, you could reset the background-color
of the first cell as follows: 如果使用CSS
:not()
不是一个选项,您可以重置第一个单元格的background-color
,如下所示:
.enty-table {
&:hover > td {
background-color: lightblue;
/* Reset the background color of td[rowspan] */
&[rowspan] {
background-color: #fff;
}
}
& > td[rowspan]:hover ~ td {
background-color: #fff; /* Reset the background */
}
}
Basically what I need is when I hover that td there will be nothing applied on the tr
基本上我需要的是当我悬停那个td时,tr上没有任何东西
Actually, you're hovering the tr
itself, not only that td
(refers to td[rowspan]
) 实际上,你正在徘徊
tr
本身,而不仅仅是那个td
(指的是td[rowspan]
)
Is it possible to go higher in the tree structure from CSS
是否有可能从CSS更高的树结构
CSS is cascading, there's no backward and/or parent selector ( yet ). CSS是级联的,没有后向和/或父选择器( 尚未 )。
As a Pure CSS way, you could use pointer-events: none;
作为纯CSS方式,您可以使用
pointer-events: none;
on the td[rowspan]
to prevent from triggering the mouse event on that element. 在
td[rowspan]
以防止触发该元素上的鼠标事件。
Otherwise, you need to use JavaScript to change all table-cells on hovering each one excluding td[rowspan]
. 否则,您需要使用JavaScript来更改除了
td[rowspan]
之外的每个表格单元td[rowspan]
。
For instance: 例如:
$('.enty-table').children('td:not(td[rowspan])').hover(function() {
$(this).siblings(':not(td[rowspan])').addBack().addClass('hover');
}, function() {
$(this).parent().children('td').removeClass('hover');
});
A couple of things you could do: 你可以做的几件事:
.enty-table {
& td:hover {
background-color: lightgrey;
}
}
.enty-table {
& tr:hover {
background-color: lightgrey;
}
}
Please try it. 请试一试。 Use class tree and change style using selected object.
使用类树并使用所选对象更改样式。 It will work fine for all "TD"s using proper tree pattern.
它将适用于使用适当树形图案的所有“TD”。
$(document).ready(function(){
$(".enty-table tr td").hover(function(){
$(this).css("background-color","yellow");
});
$(".enty-table tr td").mouseout(function(){
$(this).css("background-color","lightgray");
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.