悬停样式被 html 表格行中选定的样式覆盖

发布于 2024-10-01 06:41:59 字数 1291 浏览 10 评论 0原文

我有一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

云淡风轻 2024-10-08 06:41:59

它正在做你告诉它做的事情,就在这里:

if(document.getElementById('chk').checked)
{
    document.getElementById(row).style.background='Red';
}
else
{
    document.getElementById(row).onMouseOver = function() { this.className = 'hover'; }
}

你说,如果它被选中,那么它就是红色的。如果不是,那么它可以悬停

修复看起来像这样:

CSS:

.hover { background:blue }
.hover:hover { background:green }
.hoverChecked { background:red }
.hoverChecked:hover { background:green }

JS:

if(document.getElementById('chk').checked) {
  document.getElementById(row).className = 'hoverChecked';
}
else {
  document.getElementById(row).className = 'hover'; 
}

您可能还想修复您的 HTML:

<tr id="row1" style='background:#FFFFFF' class='hover'>

It's doing what you are telling it to do, right here:

if(document.getElementById('chk').checked)
{
    document.getElementById(row).style.background='Red';
}
else
{
    document.getElementById(row).onMouseOver = function() { this.className = 'hover'; }
}

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:

.hover { background:blue }
.hover:hover { background:green }
.hoverChecked { background:red }
.hoverChecked:hover { background:green }

JS:

if(document.getElementById('chk').checked) {
  document.getElementById(row).className = 'hoverChecked';
}
else {
  document.getElementById(row).className = 'hover'; 
}

You also may want to fix your HTML:

<tr id="row1" style='background:#FFFFFF' class='hover'>
灰色世界里的红玫瑰 2024-10-08 06:41:59

这就是你想要的,

CSS - 选项 I

 .yellow{ background:yellow; }

Javascript - 选项 I

   function Highlight(row)
{       
    if(document.getElementById('chk').checked)
    {
        document.getElementById(row).className += " yellow"document.getElementById(row).style.background='Red';
    }
    else
    {    
        document.getElementById(row).className = document.getElementById(row).className.replace(/\byellow\b/,'')
    }

    document.getElementById(row).removeAttribute("style");
        document.getElementById(row).onMouseOver = function() { this.className += 'hover';                    

    hover'; }                 
    }       
}

Javascript - 选项 II

 function Highlight(row)
{       
    if(document.getElementById('chk').checked)
    {
        document.getElementById(row).style.background='Red';
    }
    else
    {    
        document.getElementById(row).removeAttribute("style");
        document.getElementById(row).onMouseOver = function() { this.className = 'hover'; }                 
    }       
}

HTML

<tr id="row1">

This is what you want,

CSS - option I

 .yellow{ background:yellow; }

Javascript - Option I

   function Highlight(row)
{       
    if(document.getElementById('chk').checked)
    {
        document.getElementById(row).className += " yellow"document.getElementById(row).style.background='Red';
    }
    else
    {    
        document.getElementById(row).className = document.getElementById(row).className.replace(/\byellow\b/,'')
    }

    document.getElementById(row).removeAttribute("style");
        document.getElementById(row).onMouseOver = function() { this.className += 'hover';                    

    hover'; }                 
    }       
}

Javascript - Option II

 function Highlight(row)
{       
    if(document.getElementById('chk').checked)
    {
        document.getElementById(row).style.background='Red';
    }
    else
    {    
        document.getElementById(row).removeAttribute("style");
        document.getElementById(row).onMouseOver = function() { this.className = 'hover'; }                 
    }       
}

HTML

<tr id="row1">
情绪 2024-10-08 06:41:59

这是因为你的 if..else:

function Highlight(row)
{       
    if(document.getElementById('chk').checked)
    {
        document.getElementById(row).style.background='Red';
    }           
}

你根本不需要 else 路径。 CSS 将自动应用您的 :hover 样式。

It is because of your if..else:

function Highlight(row)
{       
    if(document.getElementById('chk').checked)
    {
        document.getElementById(row).style.background='Red';
    }           
}

You shouldn't need your else paths at all. Css will apply your :hover styles automatically.

爱,才寂寞 2024-10-08 06:41:59

ie8-9兼容性?

document.getElementById(row).onMouseOver = function() { this.className = 'hover'; }

ie9 不起作用。

Ie8-9 compatibility?

document.getElementById(row).onMouseOver = function() { this.className = 'hover'; }

Does not work ie9.

乖乖哒 2024-10-08 06:41:59

问题解决了!
把这一行放在html上:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">

Problem solved!
Put on html this line:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文