views:

93

answers:

4

i have the following simple code, but it doesn,t work

<ul>
  <li id="one" onmouseover="this.style.background-color='white';">
    <a href="#">home</a>
  </li>
</ul>

could you tell me why. thanks

edit:

and how can i also change the color of a tag, onmouseover of li

+5  A: 

Convert hyphens to camelCase when changing properties of the style object in JS.

backgroundColor

However, you are trying to solve this problem in the wrong way.

If you really wanted to style the list item on hover, then you probably should be using li:hover in your stylesheet. The only downside is that this won't work in IE 6 (although it is just a cosmetic effect on an ancient browser that is increasingly falling in the "Not supported" box).

That said, having a hover effect shouts "You can click now!" at the user — but only the link portion of the list item will do anything when clicked. This means that you should style the a element, not the li … but style it to fill the list item (and this will work in IE6).

Listamatic has many examples.

David Dorward
@David Dorward but how can i style a element to fill the list item, if i want to change color on hover of li, not a?
Syom
`display: block` gets you most of the way there. http://css.maxdesign.com.au/listamatic/vertical08.htm for instance.
David Dorward
@Syom: Didn't want to detract from David's answer by writing my own, but as per your edit, you can apply CSS styles to the `<a>` element using the direct descendant CSS selector: e.g. `li:hover > a { color: black; }`
Andy E
+2  A: 

it'll be onmouseover="this.style.backgroundColor='white';"

gX
+1  A: 

Why not use pure CSS for this one?

li:hover {
    background-color: #ffffff;
}

Otherwise use gX's and David Dorward's suggestion.

Koraktor
A: 

You can also use whatever:hover or a js framework (like jQuery). whatever:hover has only 3kb or so, so I guess is worth to load it :)

As a side note, I think you should take a look at this list to see how CSS styles are converted to JS.

Ionut Staicu