views:

465

answers:

5

On my CSS I have:

li.sort:hover {color: #F00;}

All my LI elements under the 'sort' class function properly when the DOM is ready.

If I create a new LI element (using mootools el.addClass(classname)) I can set the base class, but can't figure out how to add a "hover" class to it.

Any ideas?

d.

+3  A: 

The hover pseudoclass can be defined ahead of time in the stylesheet based on the classname that you're specifying. Such as:

li.classname:hover {color:#F000;}

So it's defined the same way, via the stylesheet. You would just plan ahead, knowing that you'll be defining the class name on JS-generated LI tags with a certain class, and style for it despite the fact that the list items don't exist until you create them with JavaScript.

+2  A: 

Hover class is added automatically when you add the non hover class. E.g. if you have

.MyClass { ... }

.MyClass:hover { ... }

just add the MyClass, and the MyClass:hover will work.

Kamarey
Class and id names can't start with a capital letter.
Wolfr
It works for me, so I can:)
Kamarey
XML tag names and attributes should be all lowercase, but I've never heard that IDs and class names cannot be capitalized? What specification is that from?
Calvin
Classes and ids can start with capital letters. http://www.w3.org/TR/html4/struct/global.html#h-7.5.2
David Dorward
+2  A: 

:hover is not a class, but is a pseudo-selector that will select any elements the mouse is currently hovering over. If you create an li element, and add the sort class to it, then whenever you move your mouse over the element, the li.sort:hover rule should be activated, if the browser is working correctly.

Brian Campbell
A: 

Hello,

Not all browsers will accept the hover pseudo class on all elements. You should consider using javascript for this effect. jQuery for example, makes this very easy.

majestiq
A: 

Not all browsers will accept the hover pseudo class on all elements. You should consider using javascript for this effect. jQuery for example, makes this very easy.

To be more specific, IE6 only picks up :hover styles on anchor (a) elements.

Wolfr