div onmouseout不符合预期 [英] div onmouseout not as expected

查看:72
本文介绍了div onmouseout不符合预期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文本和示例代码改编自 http://www.webdeveloper.com/forum/archive/index.php/t-65078.html ,可能无法反映实际问题:

Text and sample code adapted from http://www.webdeveloper.com/forum/archive/index.php/t-65078.html, may not reflect actual question:

我有一个div,然后是一个带有嵌套表的div:在父div之外还有一个div.

I have a div and then a div with a nested table inside it: also there is some div outside the parent div.

<div onmousemove="move()" onmouseout="out()">
  <div id="dvRep"> </div>
    <table>
        <tr><td>ITEM 1</td></tr>
        <tr><td>ITEM 2</td></tr>
        <tr><td>ITEM 3</td></tr>
        <tr><td>ITEM 4</td></tr>
        <tr><td>ITEM 5</td></tr>
    </table>
</div>

<div id="divChartPopupMenu">
    Hide me.     
</div>

每当我在div中移动鼠标时,它都会正确调用move函数,但是 onmouseout 属性无法正常运行.我以为仅当您将鼠标移离div时才会调用 out()函数,但是每当我移出表行之一时就会调用 out().因此,如果我的鼠标位于一行上,而我移至下一行,它将调用 out().我希望仅当用户离开整个div时才调用 out().有什么想法吗?

Whenever I move my mouse within the div it correctly calls the move function, but the onmouseout attribute doesn't work like I thought. I thought that the out() function would only be called if you moved your mouse off the div, but out() is called whenever I move off one of the table rows. So if my mouse is on a row and I move to the next row it calls out(). I want out() to be called only when the user moves off the entire div. any ideas?

我正在尝试的是out功能,我正在隐藏另一个div.

What I am trying is on out function I am hiding another div.

推荐答案

我建议您阅读有关javascript事件的3个阶段的文章.
http://www.quirksmode.org/js/events_order.html

i suggest you to read this article on 3 phases of javascript events.
http://www.quirksmode.org/js/events_order.html

在函数移出或移出时,可以检查srcElement(IE)或target(W3C)是否具有名为dvRep的ID.

in the function move or out, you can check if the srcElement(IE) or target(W3C) has an id called dvRep..

它应该看起来像这样:

function out(event)
{
   event.stopPropagation(); //cancel bubbling

   ele = event.target || event.srcElement
   if (ele.id === "dvRep"){  
      //your code
   }
}

这篇关于div onmouseout不符合预期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆