3

我有这样的结构:

<div id="container">

<span>
   <span></span>
</span>

<span>
   <span></span>
</span>

</div>

我需要捕捉容器的 mouseout 事件,所以我让 jquery 这样做:

$("#container").hover('',function(){ 
alert("Out"); 
});

在 Firefox / Opera 中,它只在离开 div 时触发 mouseout 功能(我想要它)。

在 IE 中,它会在鼠标点击的 div 内的每个 *-Tag 处触发 mouseout 功能。(*也许重要的是,span 标签也有 mouseover 和 out 事件)

任何人都知道如何解决这个问题?(嵌套结构无法更改,因为布局复杂)

thx4 任何想法!

4

7 回答 7

5

@evelio:没用,id 总是“容器”

我是如何解决的(到目前为止......):

信不信由你,container-div 的属性 background-color 必须设置为一种颜色。我仍然对这个事实感到非常震惊,但我尝试了几次,它只有 CSS 中的背景颜色属性才能使它工作或不工作。

并且:颜色#000000 不起作用,任何其他颜色都可以,包括“白色”

于 2009-10-07T22:12:30.857 回答
1
 $("#container").hover('',function(ev){

      alert("Out");
      if( ev.stopPropagation ) { ev.stopPropagation(); } //For 'Good' browsers
      else { ev.cancelBubble = true; } //For IE

 });

另请阅读:事件冒泡和捕获

于 2009-10-14T15:38:04.087 回答
1

The way you can solve it is adding a 1px transparant png as background.

See: IE8: Div hover only works when background color is set, very strange, why?

于 2012-03-12T16:34:17.793 回答
1

尝试这个

$("#container").mouseleave(function(){ 
alert("Out"); 
});

至于 IE,抵制蹩脚的浏览器,并在博客上写下它的彻底绝望,直到你的手指麻木为止。该浏览器导致网页设计师的时间价值低于应有的价值约 33%。尽你所能杀死它。

于 2011-03-21T08:14:27.097 回答
0

嗯,我附近没有 IE,但你可以尝试jQuery mouseout 演示(和悬停演示),如果它工作正常似乎是你的代码在其他行中的问题......最后你可以解决它:

$("#container").hover('',function()
{
    //Are you sure?
    if($(this).attr('id') == 'container')
    {
        alert('yup this is container');
    }
});
于 2009-10-07T21:26:08.857 回答
0

你有没有尝试过:

$("#container").hover('',function(){ 
    alert("Out"); 
    return false;
});

或者:

$("#container").hover('',function(e){ 
    if($(e.target).is("#container")){
        alert("Out"); 
        return false;
    }
});

或者更好:

$("#container").mouseout(function(e){ 
    if($(e.target).is("#container")){
        alert("Out"); 
        return false;
    }
});

如果您只需要鼠标悬停,则没有理由使用悬停功能。

于 2009-10-08T00:56:26.680 回答
0

我在 IE 6、7 和 8 中遇到了类似的问题。Mafka 是对的,必须设置背景颜色才能使其正常工作。如果在“容器”上设置背景颜色不可行,您仍然可以将背景颜色设置为白色并将不透明度设置为 0。

在 jQuery 中绑定 mouseover 事件之前,我使用以下 JavaScript 代码完成了此操作:

if ($.browser.msie) {
    $("#container").css({
        background: '#fff',
        opacity: 0
    });
}
于 2010-05-21T10:46:15.850 回答