87,910
社区成员
发帖
与我相关
我的任务
分享
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<div style="background:#CCCCCC;height:300px;width:300px" onmouseout="alert('mouseout')">
<div style="background:#333333; margin:50px 50px 50px 50px;height:200px;width:200px">
</div>
</div>
</body>
</html>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<div id="test" onmouseout="mouseout();" style="left:0px;top:0px;background:#CCCCCC;height:300px;width:300px">
<div style="background:#333333; margin:50px 50px 50px 50px;height:200px;width:200px">
</div>
</div>
</body>
</html>
<script>
function mouseout(){
var test=document.getElementById("test");
var e=window.event;
var left=parseInt(test.style.left);
var width=parseInt(test.style.width);
var top=parseInt(test.style.top);
var height=parseInt(test.style.height);
//利用事件触发时鼠标位置来防止该现象
if(e.clientX > left && e.clientX < (left+width) && e.clientY > top && e.clientY < (top+height)){
}else{
alert("mouseout");
}
}
</script>
onmouseleave="alert('mouseout')
(function (bool) {
//兼容FF一些方法
var html;
if (bool) {
html = window.HTMLElement.prototype;
window.__defineGetter__("event", function () {
//兼容Event对象
var o = arguments.callee;
do {
if (o.arguments[0] instanceof Event) return o.arguments[0];
} while (o = o.caller);
return null;
});
Event.prototype.__defineGetter__("fromElement", function () {
/*
这里:
onmouseover的时候relatedTarget为鼠标离开(onmouseout)的对象
onmouseout的时候relatedTarget为鼠标当前(onmouseover)的对象
*/
return this.relatedTarget;
});
html.contains = function (o) {
do {
if (o == this) return true;
} while (o = o.parentNode);
return false;
};
}
})(/Firefox/.test(window.navigator.userAgent));
window.onload = function () {
var wc = document.getElementById("ta");
wc.onmouseover = function () {
var wc = this, e = window.event;
if (!wc.contains(e.fromElement)) alert("over");
};
wc.onmouseout = function () {
var wc = this, e = window.event;
if (!wc.contains(e.toElement || e.fromElement)) alert("out");
};
};