您好,欢迎访问一九零五行业门户网

javascript 兼容FF的onmouseenter和onmouseleave的代码_javascript技巧

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
其它类似信息

推荐信息