views:

51

answers:

2

I'm trying to emulate some pseudo-classes and attribute selectors in Internet Explorer 6 and 7, such as :focus, :hover or [type=text]. So far, I've managed to add a class name to the affected elements:

$("input, textarea, select")
.hover(function(){
    $(this).addClass("hover");
}, function(){
    $(this).removeClass("hover");
})
.focus(function(){
    $(this).addClass("focus");
})
.blur(function(){
    $(this).removeClass("focus");
});

$("input[type=text]").each(function(){
    $(this).addClass("text");
});

However, I'm still forced to duplicate selector in my style sheets:

textarea:focus, textarea.focus{
}

And, to make things worse, IE6 seems to ignore all the selectors when it finds an attribute:

input[type=text], input.text{
    /* IE6 ignores this */
}

And, of course, IE6 ignores selectors with multiple classes:

input.text.focus{
    /* IE6 ignores this */
}

So I'm likely to end up with this mess:

input[type=text]{
    /* Rules here */
}
input.text{
    /* Same rules again */
}
input[type=text]:focus{
}
input.text_and_focus{
}
input.text_and_hover{
}
input.text_and_focus_and_hover{
}

My question: is there any way to read the rules or computed style defined for a CSS selector and apply it to certain elements, so I only need to maintain one set of standard CSS?

A: 

If you're already depending on javascript with your styles (otherwise, neither :focus or .focus work in IE6), I advice you to use IE7.js by Dean Edwards, which js-enhance ie6.

http://code.google.com/p/ie7-js/

Adam Kiss
I used that library some years ago and it's sort of overkill. It performs really slowly because it's very ambitious: it fixes rendering bugs and it emulates lots of CSS3 stuff I don't really need. Considering that I'm already using jQuery for the site functionality, I guess poor IE6 wouldn't be able to cope with it. Thanks for the tip, anyway. I thought IE7 was abandoned and I'm glad to see it's not.
Álvaro G. Vicario
I used `IE7.js` with `jQuery` before and if your server isn't very slow, it works okay - i mean, you have less work, your site is 'mostly' IE6-compatible and the world is nice again :)
Adam Kiss
A: 

I found this lovely piece of software:

  • ie-css3.js CSS3 pseudo-class selector emulation for Internet Explorer 5.5 - 8

It basically covers my requirements (unobtrusive code that doesn't require specific CSS rules), although it doesn't emulate input[type=text].

If you know about something else on this line, feel free to add a new answer.

Álvaro G. Vicario