欢迎来到福编程网,本站提供各种互联网专业知识!

javascript 兼容FF的onmouseenter和onmouseleave的代码

发布时间:2008-07-19 作者: 来源:转载
经过测试发现,例子1在ff下抖动的厉害,ie下稍微有点。具体原因其实就是mouseout的冒泡机制引起的。

IE下有onmouseenter和onmouseleave来解决。
可惜ff就没有。我再想,为什么这么好的功能,为什么ff不引用呢?
还有ie中的onpropertychange,哎,ff中都没有。。。

对比例子中引入了一段js,来兼容FF的onmouseenter和onmouseleave.:
复制代码 代码如下:
varxb=
{
evtHash:[],

ieGetUniqueID:function(_elem)
{
if(_elem===window){return'theWindow';}
elseif(_elem===document){return'theDocument';}
else{return_elem.uniqueID;}
},

addEvent:function(_elem,_evtName,_fn,_useCapture)
{
if(typeof_elem.addEventListener!='undefined')
{
if(_evtName=='mouseenter')
{_elem.addEventListener('mouseover',xb.mouseEnter(_fn),_useCapture);}
elseif(_evtName=='mouseleave')
{_elem.addEventListener('mouseout',xb.mouseEnter(_fn),_useCapture);}
else
{_elem.addEventListener(_evtName,_fn,_useCapture);}
}
elseif(typeof_elem.attachEvent!='undefined')
{
varkey='{FNKEY::obj_'+xb.ieGetUniqueID(_elem)+'::evt_'+_evtName+'::fn_'+_fn+'}';
varf=xb.evtHash[key];
if(typeoff!='undefined')
{return;}

f=function()
{
_fn.call(_elem);
};

xb.evtHash[key]=f;
_elem.attachEvent('on'+_evtName,f);

//attachunloadeventtothewindowtocleanuppossiblyIEmemoryleaks
window.attachEvent('onunload',function()
{
_elem.detachEvent('on'+_evtName,f);
});

key=null;
//f=null;/*DON'Tnullthisout,orwewon'tbeabletodetachit*/
}
else
{_elem['on'+_evtName]=_fn;}
},

removeEvent:function(_elem,_evtName,_fn,_useCapture)
{
if(typeof_elem.removeEventListener!='undefined')
{_elem.removeEventListener(_evtName,_fn,_useCapture);}
elseif(typeof_elem.detachEvent!='undefined')
{
varkey='{FNKEY::obj_'+xb.ieGetUniqueID(_elem)+'::evt'+_evtName+'::fn_'+_fn+'}';
varf=xb.evtHash[key];
if(typeoff!='undefined')
{
_elem.detachEvent('on'+_evtName,f);
deletexb.evtHash[key];
}

key=null;
//f=null;/*DON'Tnullthisout,orwewon'tbeabletodetachit*/
}
},

mouseEnter:function(_pFn)
{
returnfunction(_evt)
{
varrelTarget=_evt.relatedTarget;
if(this==relTarget||xb.isAChildOf(this,relTarget))
{return;}

_pFn.call(this,_evt);
}
},

isAChildOf:function(_parent,_child)
{
if(_parent==_child){returnfalse};

while(_child&&_child!=_parent)
{_child=_child.parentNode;}

return_child==_parent;
}
};
本篇文章来源于cssrain.cn原文链接:http://www.cssrain.cn/article.asp?id=952

相关推荐