javascript 相容FF的onmouseenter和onmouseleave的代碼

來源:互聯網
上載者:User


IE下有 onmouseenter和onmouseleave來解決。
可惜ff就沒有。 我再想 , 為什麼這麼好的功能,為什麼ff不引用呢?
還有ie中的onpropertychange ,哎,ff中都沒有。。。

對比例子中引入了一段js ,來相容FF的onmouseenter和onmouseleave. : 複製代碼 代碼如下:var xb =
{
evtHash: [],

ieGetUniqueID: function(_elem)
{
if (_elem === window) { return 'theWindow'; }
else if (_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); }
else if (_evtName == 'mouseleave')
{ _elem.addEventListener('mouseout', xb.mouseEnter(_fn), _useCapture); }
else
{ _elem.addEventListener(_evtName, _fn, _useCapture); }
}
else if (typeof _elem.attachEvent != 'undefined')
{
var key = '{FNKEY::obj_' + xb.ieGetUniqueID(_elem) + '::evt_' + _evtName + '::fn_' + _fn + '}';
var f = xb.evtHash[key];
if (typeof f != 'undefined')
{ return; }

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

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

// attach unload event to the window to clean up possibly IE memory leaks
window.attachEvent('onunload', function()
{
_elem.detachEvent('on' + _evtName, f);
});

key = null;
//f = null; /* DON'T null this out, or we won't be able to detach it */
}
else
{ _elem['on' + _evtName] = _fn; }
},

removeEvent: function(_elem, _evtName, _fn, _useCapture)
{
if (typeof _elem.removeEventListener != 'undefined')
{ _elem.removeEventListener(_evtName, _fn, _useCapture); }
else if (typeof _elem.detachEvent != 'undefined')
{
var key = '{FNKEY::obj_' + xb.ieGetUniqueID(_elem) + '::evt' + _evtName + '::fn_' + _fn + '}';
var f = xb.evtHash[key];
if (typeof f != 'undefined')
{
_elem.detachEvent('on' + _evtName, f);
delete xb.evtHash[key];
}

key = null;
//f = null; /* DON'T null this out, or we won't be able to detach it */
}
},

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

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

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

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

return _child == _parent;
}
};
本篇文章來源於 cssrain.cn 原文連結:http://www.cssrain.cn/article.asp?id=952

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.