relatedTarget对象介绍 (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"); }; };
改了你的一些代码的错误和不兼容性<!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=utf-8" /> <title> Untitled Document </title> <script type="text/javascript"> function initBox(){ if (!document.getElementsByTagName){ return; } var imgs = document.getElementsByTagName("img"); for (var i=0; i <imgs.length; i++){ var img = imgs[i]; if(img.getAttribute("rel")!=""){
img.onmouseover = function () {//注意不是img参数,这样的话div就一直显示在最后一个图片上了 showBox(this); } img.onmouseout=function(){//=====添加了一个onmouseout事件 var d=document.getElementById("lay"); if(d!=null) d.style.display="none"; } } } } function showBox(imgobj){ //=========下面只创建一次div================== //get mouse location var mousepoint=this.getAbsPos(imgobj); //create layer var objLay=document.getElementById("lay"); if(objLay==null) //第一次 { objLay=document.createElement("div"); objLay.setAttribute('id','lay'); objLay.style.position = 'absolute';
(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");
};
};
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> Untitled Document </title>
<script type="text/javascript">
function initBox(){
if (!document.getElementsByTagName){ return; }
var imgs = document.getElementsByTagName("img"); for (var i=0; i <imgs.length; i++){
var img = imgs[i];
if(img.getAttribute("rel")!=""){
img.onmouseover = function () {//注意不是img参数,这样的话div就一直显示在最后一个图片上了
showBox(this); }
img.onmouseout=function(){//=====添加了一个onmouseout事件
var d=document.getElementById("lay");
if(d!=null)
d.style.display="none";
}
}
}
}
function showBox(imgobj){
//=========下面只创建一次div==================
//get mouse location
var mousepoint=this.getAbsPos(imgobj);
//create layer
var objLay=document.getElementById("lay");
if(objLay==null) //第一次
{
objLay=document.createElement("div");
objLay.setAttribute('id','lay');
objLay.style.position = 'absolute';
objLay.style.backgroundColor = "#CCCCCC";
}
else
{
objLay.innerHTML="";//清空内容
objLay.style.display="";//显示层
}
//create line break
objLay.style.top = mousepoint.top;
objLay.style.left = mousepoint.left;
var linebreak = document.createElement("br");
var fullphoto = document.createElement("img");
fullphoto.src=imgobj.src;
objLay.appendChild(fullphoto);
objLay.appendChild(linebreak);
//ff时自定义属性只能用getAttribute来获取
objLay.appendChild(document.createTextNode(imgobj.getAttribute("rel")));
document.getElementsByTagName("body")[0].appendChild(objLay);
objLay.onmouseout= function () { //onmouseout事件
this.style.display="none";
}
objLay.onmouseover=function(){this.style.display="";}//onmouseout事件
} function getAbsPos(obj){
var objResult = new Object();
objResult.top = obj.offsetTop;
objResult.left = obj.offsetLeft;
objResult.width = obj.offsetWidth;
objResult.height = obj.offsetHeight;
while( obj = obj.offsetParent){
objResult.top += obj.offsetTop;
objResult.left += obj.offsetLeft;
}
return objResult;
}
function addLoadEvent(func)
{
var oldonload = window.onload;
if (typeof window.onload != 'function'){
window.onload = func;
} else {
window.onload = function(){
oldonload();
func();
}
} }
addLoadEvent(initBox);
</script>
</head> <body>
<img src="01.jpg" rel="abc" border="1" width="50" />
<img src="02.jpg" rel="def" border="1" width="50" />
<img src="03.jpg" rel="xyz" border="1" width="50" />
</body>
</html>