Change color of div with javascript in Firefox
I have a div with some text, I want to change its background color on "onmouseover" event, its working fine in Internet Explorer but not working at all in Firefox.
Please answer.
Every attempt would be respect开发者_高级运维ed.
Thanks in advance...
a Pure javascript solution for your problem tell if it works for you
<div onMouseOver="this.style.backgroundColor='#CCFF99';"
onMouseOut="this.style.backgroundColor='#FFFFFF';" ">
Hello Welcome Testing Bg color on MouseOver
</div>
Demo
jsfiddle.net/577nc/1
The following code works in 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>
The problem you may have been facing is if you set the divs background color with 'background-color'. The above code uses 'background' to set the divs color and this can then be overided with javascript.
want to change its background color on "onmouseover" event
also you can make it using the simple CSS without any javascript code:
.myDiv {
background:#ffffff;
}
.myDiv:hover {
background:#cccccc;
}
精彩评论