Click to See Complete Forum and Search --> : Is this mouseover/out possible with css?


marringi
05-22-2008, 10:23 AM
I want to use this on my page:

<td onmouseover="style.backgroundColor=#990000" onmouseout="style.backgroundColor=#003366">

But I do not want to write the color of the onmouseout (#003366). I would like some script (or if possible css) to change the color back to the original color (#990000).

Is this possible?

rpgfan3233
05-22-2008, 11:09 AM
Not in Internet Explorer 6 or earlier, but IE7 and other browsers handle it just fine. As a result, a bit of JS still needs to be applied in IE6 and older (if you care about older versions than IE6). Here is my bit, though I can't really say whether it works in IE6 or not. I'm not very good with JS, but it should do what you need.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>Blah</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<!-- the following block targets IE7 and newer as well as all non-IE browsers (excluding IE/mac) -->
<!--[if gte IE 7]><!-->
<style type="text/css">
.example { background-color: #003366 }
.example:hover { background-color: #990000 }
</style>
<!--<![endif]-->

<!-- target IE 6 and older -->
<!--[if lte IE 6]>
<script type="text/javascript">
function applyHover () {
var targetClassName = 'example';
var TDs = document.getElementsByTagName('td');
for (var i = 0; i < TDs.length; i++)
{
if (TDs[i].className == targetClassName)
{
TDs[i].style.backgroundColor = '#003366';
TDs[i].onmouseover = function () { this.style.backgroundColor = '#990000'; };
TDs[i].onmouseout = function () { this.style.backgroundColor = '#003366'; };
}
}
}
window.onload = applyHover;
</script>
<![endif]-->
</head>
<body>
<table>
<tr>
<td class="example">
Hello World!
</td>
</tr>
</table>
</body>
</html>

marringi
05-22-2008, 11:29 AM
The thing is, that I have a template page that displays rows of data from a database. Every other row is white and every other row is gray. So I cannot set the color of which the onmouseout should be. I have to set the background color of the onmouseout in this script. Don't I?

Centauri
05-22-2008, 06:48 PM
You could set the background hover using the css :hover psuedo class on the <td>, and include the suckerfish javascript to apply the hover for IE6#tableID td:hover, #tableID td.sfhover {
background-color: #990000;
}