1

Having a bit of problem debugging my jQuery code...

In order to allow hover effects on block elements (such as div) in IE, I want to use jQuery to do the trick instead of css. My jQuery code looks something like:


$(document).ready(function()
{
    $("div.foo").mouseover(function(){
            $("div.foo h3").addClass("hover");
        }).mouseout(function(){
            $("div.foo h3").removeClass("hover");
    });
});

This works as my header switch between h3 and h3.hover, BUT if I try to:


$(document).ready(function()
{
    $("div.bar").mouseover(function(){
            $(this).addClass("hover");
        }).mouseout(function(){
            $(this).removeClass("hover");
    });
});

This won't work in all versions of IE. Does it mean IE has trouble detecting multiple classes on 1 element (which is div.bar.hover)? Thanks in advance.

EDIT:

After examined the code, I realised the problem lies in a conflict with javascript curvycorners-2.0.4 (which is another IE hack) that were also applied to this element.

1
  • 1
    div is not a non-block element, by the way. Commented Nov 21, 2009 at 10:12

4 Answers 4

3

use toggleClass instead:

$(document).ready(function()
{
    $("div.bar").mouseover(function(){
            $(this).toggleClass("hover");
        }).mouseout(function(){
            $(this).toggleClass("hover");
    });
});

It will add class if not there, and remove if already applied.

And correct: div.bar.hover is not valid CSS selector for IE6. instead do something like that: #myPanel div.hover.

Sign up to request clarification or add additional context in comments.

Comments

2

Even shorter:

$('div.bar').on('mouseenter mouseleave', function () {
    $(this).toggleClass('hover');
});​

Comments

1

True, IE6 cant handle multiple classes in CSS, f.ex:

div.one.two{color:red}

wont work for <div class="one two">red</div>

Update: It might be a bubbling issue as well, try using the .hover helper http://docs.jquery.com/Events/hover to prevent that.

1 Comment

I tested on all versions, not just IE6.
1

The sample provided by you works flawless in IE6 (can't check IE7/8). Check here

http://jsbin.com/uyopi

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.