简体   繁体   English

css悬停在一个不起作用的表行上

[英]css hover on a table row not working

I am trying to achieve a hover effect on a table row and I have the following code on my css. 我试图在表行上实现悬停效果,我在我的CSS上有以下代码。

.datatable tr.row:hover, .datatable tr.altrow:hover {
    color: #000;
    background-color: #FFFACD;
    background-image: url(../Images/bullet.gif);
    background-repeat: no-repeat;
}

But I do not see that being applied at all. 但我认为根本没有应用。 My questions are 我的问题是

  1. Why is it not working? 为什么不起作用?
  2. How to correct this? 怎么纠正这个?

For clarity here is the fiddle http://jsfiddle.net/naveen/UPhRE/ 为清楚起见,这里是小提琴http://jsfiddle.net/naveen/UPhRE/
Please don't mind the images inside the css. 请不要介意CSS内的图像。 All is well there. 一切都很好。

You are setting backgrounds on the individual table cells ( td ), which is rendered on top of the tr . 您正在单个表格单元格( td )上设置背景,该单元格呈现在tr顶部。

If you have two choices: 如果您有两个选择:

1) Move all row background styling to the tr s. 1)将所有行背景样式移动到tr

2) Update your CSS to look like this: 2)更新你的CSS看起来像这样:

.datatable tr.row:hover td, .datatable tr.altrow:hover td {
    color: #000;
    background-color: #FFFACD;
    background-image: url(../Images/bullet.gif);
    background-repeat: no-repeat;
}

This will render the color on the row: http://jsfiddle.net/R9YGw/3/ 这将呈现行上的颜色: http//jsfiddle.net/R9YGw/3/

Update : Included update for image on first cell only: 更新 :仅包括第一个单元格上的图像更新:

.datatable tr.row:hover td, .datatable tr.altrow:hover td {
    color: #000;
    background-color: #FFFACD;
}
.datatable tr.row:hover td:first-child, .datatable tr.altrow:hover td:first-child {
    background-image: url(../Images/bullet.gif);
    background-repeat: no-repeat;
}

The problem you have is that you set a background color on the td elements, which are further down the DOM tree and thus the color of these takes precedence. 您遇到的问题是您在td元素上设置了背景颜色,这些元素位于DOM树的下方,因此这些元素的颜色优先。

If you apply the color to just the row you will see the hover appearing: 如果您将颜色应用于该行,您将看到悬停显示:

.datatable .row
{
    background-color: #FFFFFF;
}
.datatable .altrow
{
    background-color: #EDF5FF;
}

You can hover it, actually, but it won't work great in older IE's. 实际上,你可以悬停它,但它在旧的IE中不会很好用。 Your problem is that your initial (non-hover) color is assigned to the TD elements, not the TRs. 您的问题是您的初始(非悬停)颜色被分配给TD元素,而不是TR。 Modify your CSS so it does this: 修改你的CSS,这样做:

.datatable tr.row:hover, .datatable tr.altrow:hover
.datatable tr.row:hover td, .datatable tr.altrow:hover td
{
    color: #000;
    background-color: #FFFACD;
    background-image: url(../Images/bullet.gif);
    background-repeat: no-repeat;
}

And it works okay. 它工作正常。 Not sure how the image part will look in each TD though, so you may need to do more tweaks for that part. 不确定图像部分在每个TD中的外观如何,因此您可能需要对该部分进行更多调整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM