font-awesome图标 - onmouseover事件更改颜色

时间:2018-05-21 11:34:00

标签: css asp.net font-awesome onmouseover onmouseout

我有一个ASP.NET gridview控件,在结束列中有一个字体很棒的垃圾桶图标。

鼠标悬停上我希望颜色变为红色,然后再返回默认 onmouseout

这是asp.net gridview中的template field

<asp:TemplateField HeaderText="Delete?" ItemStyle-HorizontalAlign="Center">
    <ItemTemplate>
        <span ID="lnkDelete" style="border:none" onclick="return confirm('Are you sure you want to delete this Performance Review?')"
                    runat="server" ItemStyle-CssClass="fa fa-trash-0" 
                onmouseover="this.style='color:red;'"    
                CommandName="Delete">
                <i  class="fa fa-trash-o" onmouseover="script:this.style='color:red; font-size:24px'"  onmouseout="script:this.style='color:black; font-size:24px'"
                    style="font-size:24px;"></i> 
            </span>

    </ItemTemplate>
</asp:TemplateField>

并在IE中呈现为

<span id="ctl00_m_g_10488b48_1486_45be_8a9c_efc307c0cb4b_ctl00_grdPR_ctl04_lnkDelete" style="border: currentColor;" 
onclick="return confirm('Are you sure you want to delete this Performance Review?')" CommandName="Delete" ItemStyle-CssClass="fa fa-trash-0">
<i class="fa fa-trash-o" style="font-size: 24px;" onmouseover="script:this.style='color:red; font-size:24px'"></i> 
                    </span>

但是当我将鼠标悬停在垃圾桶图标上时,我显然错了,因为没有任何变化。

我也尝试在我的页面中使用css hover样式,但同样没有任何反应:

<style>
.fa fa-trash-o{
  color: black
}
.fa fa-trash-o:hover{
  color:red;
}
</style>

所以我哪里错了?

2 个答案:

答案 0 :(得分:1)

请参阅下面的代码,在.课程之前忘记fa-trash-o
选择器在css中:https://www.w3schools.com/cssref/css_selectors.asp

.fa-trash-o{
  color: black
}
.fa-trash-o:hover{
  color:red;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<span>
    <i class="fa fa-trash-o" style="font-size: 24px;"></i> 
</span>

答案 1 :(得分:1)

您只能通过CSS实现此目的。问题是你在css中使用了错误的选择器。 fafa-trash-o类位于同一元素中。因此,您需要删除fafa-trash-o之间的空格,并将.放在它们之前,因为它们是类

.fa.fa-trash-o {
  color: black
}

.fa.fa-trash-o:hover {
  color: red;
}
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
<i class="fa fa-trash-o"></i>