悬停样式被 html 表格行中选定的样式覆盖
我有一个简单的 html 表,其中有几行。实现了以下内容
1)我为该行的鼠标悬停事件指定了样式,以便一旦鼠标移到该行上,该行就会突出显示。
2)每一行也有一个复选框。选择此选项后,复选框所在的行也需要以不同的颜色显示。
问题是,一旦单击复选框后行背景发生变化,应用于该行 onmoseover 事件的悬停样式将不再适用。
以下是代码;
<html>
<head></head>
<style type="text/css">
tr { background: blue; }
tr:hover { background:green; }
</style>
<script type="text/javascript">
function Highlight(row)
{
if(document.getElementById('chk').checked)
{
document.getElementById(row).style.background='Red';
}
else
{
document.getElementById(row).onMouseOver = function() { this.className = 'hover'; }
}
}
</script>
<body>
<table>
<tr><th>Name</th> <th>Age</th></tr>
<tr id="row1" bgcolor="#FFFFFF" onMouseOver="className='hover';">
<td><input type="checkbox" id="chk" onclick="javascript:Highlight('row1');"</td>
<td>25</td></tr>
<tr id="row2"><td>aaaaaa</td><td>25</td></tr>
<tr id="row3"><td>aaaaaa</td><td>25</td></tr>
</table>
</body>
</html>
如果您能给我一个解决方案,我将不胜感激。
I have a simple html table which has several rows. the following is implemented
1) I have given style for mouseover event of the row so that it will be highlighted once the mouse moves over it.
2)Each row has a checkbox as well. once this is selected, the row which the check box is in need to be shown in a different color as well.
The problem is that, once the row background changes after clicking the checkbox, the hover style applied to the onmoseover event of the row no longer applies.
Following is the code;
<html>
<head></head>
<style type="text/css">
tr { background: blue; }
tr:hover { background:green; }
</style>
<script type="text/javascript">
function Highlight(row)
{
if(document.getElementById('chk').checked)
{
document.getElementById(row).style.background='Red';
}
else
{
document.getElementById(row).onMouseOver = function() { this.className = 'hover'; }
}
}
</script>
<body>
<table>
<tr><th>Name</th> <th>Age</th></tr>
<tr id="row1" bgcolor="#FFFFFF" onMouseOver="className='hover';">
<td><input type="checkbox" id="chk" onclick="javascript:Highlight('row1');"</td>
<td>25</td></tr>
<tr id="row2"><td>aaaaaa</td><td>25</td></tr>
<tr id="row3"><td>aaaaaa</td><td>25</td></tr>
</table>
</body>
</html>
Appreciate if you could give me a solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
它正在做你告诉它做的事情,就在这里:
你说,如果它被选中,那么它就是红色的。如果不是,那么它可以
悬停
。修复看起来像这样:
CSS:
JS:
您可能还想修复您的 HTML:
It's doing what you are telling it to do, right here:
You're saying, if it's checked, then it's red. If it's not, then it's
hover
-able.To fix would look something like this:
CSS:
JS:
You also may want to fix your HTML:
这就是你想要的,
CSS - 选项 I
Javascript - 选项 I
Javascript - 选项 II
HTML
This is what you want,
CSS - option I
Javascript - Option I
Javascript - Option II
HTML
这是因为你的 if..else:
你根本不需要 else 路径。 CSS 将自动应用您的 :hover 样式。
It is because of your if..else:
You shouldn't need your else paths at all. Css will apply your :hover styles automatically.
ie8-9兼容性?
ie9 不起作用。
Ie8-9 compatibility?
Does not work ie9.
问题解决了!
把这一行放在html上:
Problem solved!
Put on html this line: