在IE的全系列中都实现了mouseenter和mouseleave事件,可是在早期的w3c浏览器中却没有实现这两个事件。有时候,咱们须要使用node
mouseenter事件来防止子元素的冒泡,这就涉及到事件兼容性的问题了。浏览器
先比较mouseenter和mouseover的异同点,当从元素外围进入元素内部时同时触发mouseover和mouseenter事件,可是在元素内部,dom
鼠标进入元素子节点时会继续触发mouseover事件,该事件是能够向上冒泡的;对于mouseenter则不会冒泡,固然也不会触发该事件。this
mouseleave亦然。spa
用mouseover来模拟mouseenter的关键在于利用事件的relatedTarget断定鼠标是否在元素内部移动,这也涉及到dom元素contain()code
的实现。为了高效的实现contain方法,尽可能使用浏览器的原生API,若是没有则只能向上回溯。blog
function contain(p,c){ if(p == c)return false; if(p.compareDocumentPosition){ return !!(p.compareDocumentPosition(c) & 16); }else if(p.contains){ return p.contains(c); } var cur; while(c = c.parentNode){ if(c.nodeType == 3 || c.nodeType == 8) continue; if(c !== p) continue; else{ return true; } } return false; }
而后着重修复mouseover事件:seo
var fixMouseenter = function(el,fn){ return window.VBArray ? { el: el, type: 'mouseenter', fn: fn } : { el: el, type: 'mouseover', fn: function(e){ !contain(el,e.relatedTarget) && fn.call(this,arguments); } }; }; var fixMouseleave = function(el,fn){ return window.VBArray ? { el: el, type: 'mouseleave', fn: fn } : { el: el, type: 'mouseout', fn: function(e){ !contain(el,e.relatedTarget) && fn.call(this,arguments); } }; };
这样对于非IE浏览器都进行事件修复,可是缺点也有很多,就是新版本的w3c浏览器都已经实现了这两个事件,因此咱们就没有必要事件
在进行事件修复。get