Javascript 表滚动高亮度更改文本颜色

Javascript 表滚动高亮度更改文本颜色,javascript,html,Javascript,Html,您好,我有一个php填充的表,它有交替的行颜色,工作正常,我现在要做的是改变表行中文本高亮显示时的颜色 下面的代码显示了表格高亮显示,虽然效果很好,但我无法更改颜色元素 <tr class="active" bgcolor="#363636" onMouseOver="this.bgColor='gold';" onMouseOut="this.bgColor='#363636';" onMouseOver="this.color='#fff';"> 使用this.style.

您好,我有一个php填充的表,它有交替的行颜色,工作正常,我现在要做的是改变表行中文本高亮显示时的颜色

下面的代码显示了表格高亮显示,虽然效果很好,但我无法更改颜色元素

 <tr class="active"  bgcolor="#363636" onMouseOver="this.bgColor='gold';" onMouseOut="this.bgColor='#363636';" onMouseOver="this.color='#fff';">

使用
this.style.color
而不是
this.color
,您可以在同一属性中设置它:

onMouseOver="this.bgColor='gold'; this.style.color='#fff';"
顺便说一句,尽量避免像那样使用内联javascript,这样就可以将html/css和javascript代码分开

您可以使用css:

tr:hover {
    color: White;
}
您的所有项目都可以用CSS重做

tr {
  background-color: #363636;
}

tr:hover {
  background-color: Gold;
  color: White;
}

您有两个冲突的
onMouseOver
属性。与其这样做,为什么不使用
:hover
伪类

tr {
    background-color: #363636;
}

tr:hover {
  background-color: gold;
  color: #ffffff;
}
如果您确实想在MouseOver上使用
onMouseOver
,可以执行以下操作:

oneMouseOver="this.bgColor='gold'; this.color='#ffffff';";

根据每行中是否有多列,可以通过几种不同的方式更新文本样式。我在中测试了这两个,它们都有效

如果只希望在鼠标悬停特定单元格时更新颜色:

 <tr class="active"  bgcolor="#363636" onMouseOver="this.bgColor='gold';" onMouseOut="this.bgColor='#363636';" >
      <td onMouseOver="this.style.color='#fff';" onMouseOut="this.style.color='#000';">
        hey
      </td>
    </tr>
<tr class="active"  bgcolor="#363636" 
   onMouseOver="this.bgColor='gold';this.style.color='#fff';" 
   onMouseOut="this.bgColor='#363636';this.style.color='#000';">
如果要在鼠标悬停行中的任何单元格时更新颜色:

 <tr class="active"  bgcolor="#363636" onMouseOver="this.bgColor='gold';" onMouseOut="this.bgColor='#363636';" >
      <td onMouseOver="this.style.color='#fff';" onMouseOut="this.style.color='#000';">
        hey
      </td>
    </tr>
<tr class="active"  bgcolor="#363636" 
   onMouseOver="this.bgColor='gold';this.style.color='#fff';" 
   onMouseOut="this.bgColor='#363636';this.style.color='#000';">


hi than如果我将鼠标悬停在列上而不是行上,实际上只会更改列的颜色。@user1691024是的,这就是为什么我根据需要提供了两个不同的示例。当鼠标移到行上时,底部的示例应该有效,而不仅仅是一个单元格。@user1691024下面是底部示例的JSFIDLE