当前位置: 代码迷 >> JavaScript >> mouseover 有关问题
  详细解决方案

mouseover 有关问题

热度:519   发布时间:2012-03-31 13:13:26.0
mouseover 问题
一个DIV鼠标在上面移动,只要一直在范围内,只会触发一个onmouseover事件

但是假如这个DIV中还嵌套有n个子DIV,那么在这n个子DIV之间移动的时候,每跨越一次DIV就是触发一次onmouseover事件。

代码如下:


HTML code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<script>
var times = 0;
function show(){

times = times+1;
document.getElementById("times").innerHTML=times;    

}
</script>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

<body>
<label id="times"></label>
<div id="popWin" onMouseOver="show();" style="width:300px;border:50px solid #06F;height:200px;">
<div id="popWin2" style="width:150px;height:200px;float:left;background:#777;">
</div>
<div id="popWin3" style="width:150px;height:200px;float:left;background:#222;">
</div>
</div>

<br/>
<div id="popWin4" onMouseOver="show();" style="width:300px;border:50px solid #06F;height:200px;">
</div>

</body>
</html>


请高手指点这是个什么情况,如何避免多次触发,感觉不像冒泡,因为子DIV上没有onmouseover事件

------解决方案--------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<script>
var times = 0;
function show(ele){
var fun=arguments.callee.caller;
var evt=fun.arguments[0];
if(evt){//非IE 
var that=evt.relatedTarget;
var pos=ele.compareDocumentPosition(that);
if(pos==0||pos==20){
return;
}
}else{//IE
var that=window.event.fromElement;
if(ele==that||ele.contains(that)){
return;
}
}
times = times+1;
document.getElementById("times").innerHTML=times;
}
</script>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

<body>
<label id="times"></label>
<div id="popWin" onMouseOver="show(this);" style="width:300px;border:50px solid #06F;height:200px;">
<div id="popWin2" style="width:150px;height:200px;float:left;background:#777;">
</div>
<div id="popWin3" style="width:150px;height:200px;float:left;background:#222;">
</div>
</div>

<br/>
<div id="popWin4" onMouseOver="show(this);" style="width:300px;border:50px solid #06F;height:200px;">
</div>

</body>
</html>
------解决方案--------------------
HTML code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script>
        var times = 0;
        function show() {
            times = times + 1;
            document.getElementById("times").innerHTML = times;
        }
    </script>
    
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
</head>
<body>
    <label id="times">
    </label>
    <div id="popWin" onmouseover="show();" style="width: 300px; border: 50px solid #06F;
        height: 200px;">
        <div id="popWin2" onmouseover="event.cancelBubble=true;" style="width: 150px; height: 200px; float: left; background: #777;">
        </div>
        <div id="popWin3" onmouseover="event.cancelBubble=true;" style="width: 150px; height: 200px; float: left; background: #222;">
        </div>
    </div>
    <br />
    <div id="popWin4" onmouseover="show();" style="width: 300px; border: 50px solid #06F;
        height: 200px;">
    </div>
</body>
</html>