CSS悬停的特异性

4
我遇到了CSS特异性问题。
如果没有账单,我想要对<td>样式进行颜色上的区分,因此我使用if语句设置了样式。如果没有账单,则样式为style = "id=\"no-bills\"";。我已经使用CSS使其奏效。现在,如果用户将鼠标悬停在上面,我希望将背景变红-因此,我修改了CSS并添加了#bill-list #no-bills td:hover,其中td:hover以提高其特异性能够在悬停时添加一些额外的效果。不幸的是,这并不起作用(背景仍然保持不变)。
以下是我的CSS:
#bill-list
{
    font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
    font-size: 12px;
    /*margin: 45px;*/
    width: 100%;
    text-align: left;
    border-collapse: collapse;
}
#bill-list th
{
    font-size: 13px;
    font-weight: normal;
    padding: 8px;
    background: #b9c9fe;
    border-top: 4px solid #aabcfe;
    border-bottom: 1px solid #fff;
    color: #039;
}
#bill-list td
{
    padding: 8px;
    background: #e8edff; 
    border-bottom: 1px solid #fff;
    color: #669;
    border-top: 1px solid transparent;
}
#bill-list tr:hover td
{
    background: #d0dafd;
    color: #339;
}

#bill-list #bill td
{
    background: white;
}

#bill-list #no-bills
{
    background: #FFCC99;
}
#bill-list #no-bills td:hover
{
    color: orange;
    background: red /*#FFCC66*/;
}

这是我的代码(片段):

这里是我的代码(片段):

<table id="bill-list">
<thead>
    <tr>
        <% for (int i=0; i<vecHeader.size(); i++) { %>
            <th><%=vecHeader.get(i) %></th>
        <% } %>
    </tr>
</thead>

<tbody>
    <%  int uniqueId = 0;
        for (int i=0; i < vecValue.size(); i++) { 
        boolean hasBills = true;
        String style = "";
        if ( vecBills.get(i) == null || vecBills.get(i).size() == 0 ) {
            hasBills = false;
            style = "id=\"no-bills\"";
        }
        %>
        <tr id="bill<%=i%>" onclick="javascript:showBillDetails(<%=i%>)">
            <% for (int j=0; j < vecHeader.size(); j++) {
                prop = (Properties)vecValue.get(i);
                %>
                <td <%=style%>><%=prop.getProperty((String)vecHeader.get(j), "&nbsp;") %>&nbsp;</td>
            <% } %>
        </tr>
...
...

有人有任何想法吗?

2个回答

4
你应该发布HTML代码,而不是生成HTML代码的代码,因为这在此处是不相关的。
第一个问题:你确定同一页上不能有两个ID为no-bills的元素吗?
然后是你的问题:你试图给#no-bills的后代td:hover设置样式。后者是相同的元素,不是祖先元素!你应该给#bill-list #no-bills:hover设置样式,它似乎是一个被悬停的元素。

成功了 - 谢谢!(哦,是的,我应该发布HTML)。 - Jarrett

1

尝试

#bill-list #no-bills:hover

替代

#bill-list #no-bills td:hover

如果有多个无账单,应该使用类而不是ID。

是的,我刚把它们改成了类,因为有多个无账单。谢谢! - Jarrett

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接