mouseout事件的问题

Jua*_*uan 6 html javascript events scriptaculous

我正在使用JavaScript来隐藏图像并显示隐藏在其下的一些文本.但是,当文本显示时,如果你滚动它,它会触发容器上的mouseout事件,然后隐藏文本并再次显示图像,它只是进入一个奇怪的循环.

html看起来像这样:

<div onmouseover="jsHoverIn('1')"
     onmouseout="jsHoverOut('1')">
    <div id="image1" />
    <div id="text1" style="display: none;">
        <p>some content</p>
        <p>some more content</p>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

和javascript(它使用scriptaculous):

function jsHoverIn(id) {
  if(!visible[id]) {
    new Effect.Fade ("image" + id, {queue: { position: 'end', scope: id } });
    new Effect.Appear ("text" + id, {queue: { position: 'end', scope: id } });
    visible[id] = true;
  }
}
function jsHoverOut (id) {
  var scope = Effect.Queues.get(id);
  scope.each(function(effect) { effect.cancel(); });

  new Effect.Fade ("text" + id, {queue: { position: 'end', scope: id } });
  new Effect.Appear ("image" + id, {queue: { position: 'end', scope: id } });
  visible[id] = false;
}
Run Code Online (Sandbox Code Playgroud)

这看起来很简单,但我无法绕过它.

Spo*_*ser 5

我给容器div:

position: relative;
Run Code Online (Sandbox Code Playgroud)

并在容器中添加第三个div(应该是容器的最后一个子节点):

position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
Run Code Online (Sandbox Code Playgroud)

并在此div上捕获mouseover和mouseout事件.

因为它没有子元素,所以不应该将虚假鼠标悬停和mouseout事件传播给它.

编辑:

我认为发生的是,当光标从父元素移动到子元素时,父元素上发生mouseout事件,并且子元素上发生鼠标悬停事件.但是,如果子元素上的mouseover处理程序未捕获事件并停止传播,则父元素也将接收mouseover事件.