繁体   English   中英

在Firefox中使用javascript更改div的颜色

[英]Change color of div with javascript in Firefox

我有一个带有一些文本的div,我想在“onmouseover”事件上更改它的背景颜色,它在Internet Explorer中正常工作但在Firefox中根本不工作。

请回答。

每一次尝试都会得到尊重。

提前致谢...

一个纯粹的JavaScript解决方案,为您的问题告诉它是否适合您

<div onMouseOver="this.style.backgroundColor='#CCFF99';"
onMouseOut="this.style.backgroundColor='#FFFFFF';" ">

Hello Welcome Testing Bg color on MouseOver 
</div>

演示

jsfiddle.net/577nc/1

以下代码适用于Firefox:

       <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <style>
            #div_to_change_colour {
                background: rgb(255, 0, 0);
            }
        </style>
        <script type="text/javascript">
            function changeColor(objectPassedIn){
                objectPassedIn.style.background = '#CCC';
                objectPassedIn.style.width = '200px';
            }
        </script>
        <title></title>
    </head>
    <body>
        <div id="div_to_change_colour" onmouseover="changeColor(this)">
            text inside div
        </div>
</body>
</html>

您可能遇到的问题是如果您将divs背景颜色设置为'background-color'。 上面的代码使用'background'来设置div颜色,然后可以用javascript覆盖它。

想要在“onmouseover”事件中更改其背景颜色

你也可以使用简单的CSS而不需要任何javascript代码:

.myDiv {
 background:#ffffff;
}
.myDiv:hover {
  background:#cccccc;
}

Javascript变色<div>有条件</div><div id="text_translate"><p>所以我有这个代码块,我需要 div 的颜色在紫色和白色之间“闪烁”,以实现条件“MECHANIC ON LINE”。 我知道在 switch 语句之外我正在实现的代码正常工作,但在 switch 语句中我不确定。 这也是在公司服务器上,我不确定后端的某些进程如何处理页面的更新。 无论如何,如果有人知道我在 switch 语句中实现我的代码是否不正确,请告诉我。 此外,第一个代码片段取自下面更大的片段,它是我关心的 switch 语句的片段。 最后,我还想提一下我已经将时间间隔设置为彼此不同的整数,例如“setInterval(function, 10 * 1000), setInterval(function, 20 * 2000)”。</p><pre> case "MECHANIC ON LINE": colorTitle[i].style.color = 'rgb(127.5, 127.5, 127.5)'; if(colorBackground[l].style.background === 'rgb(123,31,162)') { setInterval(colorBackground[l].style.background = 'rgb(255,255,255)',10 * 1000); } else { setInterval(colorBackground[l].style.background = 'rgb(123,31,162)',10 * 1000); } break;</pre><pre> for(var i = 0; i &lt; colorTitle.length; i++) { for(var l = i; l &lt; colorBackground.length; l++) { switch(colorTitle[i].textContent) { case "RUNNING": colorTitle[i].style.color = 'rgb(0, 255, 0)'; colorBackground[l].style.background = 'rgb(0, 255, 0)'; break; case "LINE DOWN": colorTitle[i].style.color = 'rgb(255, 0, 0)'; colorBackground[l].style.background = 'rgb(255, 0, 0)'; break; case "OFFLINE": colorTitle[i].style.color = 'rgb(127.5, 127.5, 127.5)'; colorBackground[l].style.background = 'rgb(127.5, 127.5, 127.5)'; break; case "MECHANIC ON LINE": colorTitle[i].style.color = 'rgb(127.5, 127.5, 127.5)'; if(colorBackground[l].style.background === 'rgb(123,31,162)') { setInterval(colorBackground[l].style.background = 'rgb(255,255,255)',10 * 1000); } else { setInterval(colorBackground[l].style.background = 'rgb(123,31,162)',10 * 1000); } break; default: colorTitle[i].style.color = 'rgb(255, 255, 255)'; colorBackground[l].style.background = 'rgb(255, 255, 255)'; break; } } }</pre></div>

[英]Javascript color change <div> on condition

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用javascript更改div颜色 Javascript变色<div>有条件</div><div id="text_translate"><p>所以我有这个代码块,我需要 div 的颜色在紫色和白色之间“闪烁”,以实现条件“MECHANIC ON LINE”。 我知道在 switch 语句之外我正在实现的代码正常工作,但在 switch 语句中我不确定。 这也是在公司服务器上,我不确定后端的某些进程如何处理页面的更新。 无论如何,如果有人知道我在 switch 语句中实现我的代码是否不正确,请告诉我。 此外,第一个代码片段取自下面更大的片段,它是我关心的 switch 语句的片段。 最后,我还想提一下我已经将时间间隔设置为彼此不同的整数,例如“setInterval(function, 10 * 1000), setInterval(function, 20 * 2000)”。</p><pre> case "MECHANIC ON LINE": colorTitle[i].style.color = 'rgb(127.5, 127.5, 127.5)'; if(colorBackground[l].style.background === 'rgb(123,31,162)') { setInterval(colorBackground[l].style.background = 'rgb(255,255,255)',10 * 1000); } else { setInterval(colorBackground[l].style.background = 'rgb(123,31,162)',10 * 1000); } break;</pre><pre> for(var i = 0; i &lt; colorTitle.length; i++) { for(var l = i; l &lt; colorBackground.length; l++) { switch(colorTitle[i].textContent) { case "RUNNING": colorTitle[i].style.color = 'rgb(0, 255, 0)'; colorBackground[l].style.background = 'rgb(0, 255, 0)'; break; case "LINE DOWN": colorTitle[i].style.color = 'rgb(255, 0, 0)'; colorBackground[l].style.background = 'rgb(255, 0, 0)'; break; case "OFFLINE": colorTitle[i].style.color = 'rgb(127.5, 127.5, 127.5)'; colorBackground[l].style.background = 'rgb(127.5, 127.5, 127.5)'; break; case "MECHANIC ON LINE": colorTitle[i].style.color = 'rgb(127.5, 127.5, 127.5)'; if(colorBackground[l].style.background === 'rgb(123,31,162)') { setInterval(colorBackground[l].style.background = 'rgb(255,255,255)',10 * 1000); } else { setInterval(colorBackground[l].style.background = 'rgb(123,31,162)',10 * 1000); } break; default: colorTitle[i].style.color = 'rgb(255, 255, 255)'; colorBackground[l].style.background = 'rgb(255, 255, 255)'; break; } } }</pre></div> JavaScript通过链接更改div颜色 动态改变 Div 的颜色 - Javascript 在JavaScript中将鼠标悬停时更改div颜色 CSS div样式,javascript画布颜色更改 Javascript:如何更改ContentEditable div占位符颜色 Javascript 每次单击时更改 div 颜色 JavaScript单击更改div“数组”的颜色? 使用 javascript 更改 bootstrap-div 边框颜色
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM