為非IE瀏覽器添加mouseenter,mouseleave事件的實現(xiàn)代碼
先了解幾個事件對象屬性
target 指事件源對象,點擊嵌套元素最里層的某元素,該元素就是target。IE6/7/8對應(yīng)的是srcElement。
currentTarget 指添加事件handler的元素本身,如el.addEventListener中el就是currentTarget。IE6/7/8沒有對應(yīng)屬性,可在handler內(nèi)使用this來替代如evt.currentTarget = this。
relativeTarget 指事件相關(guān)的元素,一般用在mouseover,mouseout事件中。IE6/7/8中對應(yīng)的是fromElement,toElement。
mouseenter ,mouseleave IE9中仍然支持,另見 Greg Reimer 的博文 Goodbye mouseover, hello mouseenter 。
mouseenter與mouseover區(qū)別在于:在元素內(nèi)部移動時mouseenter不會觸發(fā)。如下
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>mouseerter與mouseover區(qū)別(IE下測試)</title>
</head>
<body>
<div id="result" style="position:absolute;right:100px;top:5px;width:250px;height:400px;border:2px solid gray;overflow:auto;">
</div>
<h3>1,鼠標(biāo)在div[id=parent1]內(nèi)部移動時也會觸發(fā)mouseover事件</h3>
<div id="parent1" style="width:400px;border:1px solid gray;padding:5px;">
<div id="child11" style="width:100px;height:100px;background:gold;float:left;">Child11</div>
<div id="child12" style="width:100px;height:100px;background:gold;float:right;">Child12</div>
<div style="clear:both;"></div>
</div>
<br/>
<h3>2,鼠標(biāo)在div[id=parent2]內(nèi)部移動時也不會觸發(fā)mouseenter事件</h3>
<div id="parent2" style="width:400px;border:1px solid gray;padding:5px;">
<div id="child21" style="width:100px;height:100px;background:gold;float:left;">Child21</div>
<div id="child22" style="width:100px;height:100px;background:gold;float:right;">Child22</div>
<div style="clear:both;"></div>
</div>
<script type="text/javascript">
function on(el,type,fn){
el.addEventListener ? el.addEventListener(type, fn, false) : el.attachEvent('on' + type, fn);
}
function $(id){
return document.getElementById(id);
}
var p1 = $('parent1'),
p2 = $('parent2');
function fn(e){
var d = document.createElement('div');
d.innerHTML = e.type;
$('result').appendChild(d);
}
on(p1,'mouseover',fn);
on(p2,'mouseenter',fn);
</script>
<body>
</html>
了解了這三個屬性的意義后,實現(xiàn)起來就簡單了:
E = function(){
function elContains(a, b){
try{
return a.contains ? a != b && a.contains(b) : !!(a.compareDocumentPosition(b) & 16);
}catch(e){}
}
function addEvt(el, type, fn){
function fun(e){
var a = e.currentTarget, b = e.relatedTarget;
if(!elContains(a, b) && a!=b){
fn.call(e.currentTarget,e);
}
}
if(el.addEventListener){
if(type=='mouseenter'){
el.addEventListener('mouseover', fun, false);
}else if(type=='mouseleave'){
el.addEventListener('mouseout', fun, false);
}else{
el.addEventListener(type, fn, false);
}
}else{
el.attachEvent('on' + type, fn);
}
}
return {addEvt:addEvt};
}();
測試代碼:
[Ctrl+A 全選 注:引入外部Js需再刷新一下頁面才能執(zhí)行]
- 淺談JQ中mouseover和mouseenter的區(qū)別
- 關(guān)于事件mouseover ,mouseout ,mouseenter,mouseleave的區(qū)別
- 快速移動鼠標(biāo)觸發(fā)問題及解決方法(ECharts外部調(diào)用保存為圖片操作及工作流接線mouseenter和mouseleave)
- Jquery利用mouseenter和mouseleave實現(xiàn)鼠標(biāo)經(jīng)過彈出層且可以點擊
- 跨瀏覽器的 mouseenter mouseleave 以及 compareDocumentPosition的使用說明
- javascript 兼容FF的onmouseenter和onmouseleave的代碼
- javascript中mouseenter與mouseover的異同
相關(guān)文章
JavaScript操作DOM元素的childNodes和children區(qū)別
這篇文章主要介紹了JavaScript操作DOM元素的childNodes和children區(qū)別,本文直接給出測試代碼和運行效果來講解它們之間的區(qū)別,需要的朋友可以參考下2015-04-04
詳解JavaScript如何實現(xiàn)并發(fā)控制的Promise隊列調(diào)度器
Promise是JavaScript中一種處理異步操作的技術(shù),提供了一種更優(yōu)雅的方式來處理并發(fā)任務(wù)和串行化操作,本文主要介紹了實現(xiàn)并發(fā)控制的Promise隊列調(diào)度器的方法,需要的可以參考一下2023-07-07
javascript實現(xiàn)table單元格點擊展開隱藏效果(實例代碼)
這篇文章主要介紹了javascript實現(xiàn)table單元格點擊展開隱藏效果的實例代碼講解,代碼簡單易懂,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-04-04
JavaScript實現(xiàn)數(shù)組在指定位置插入若干元素的方法
這篇文章主要介紹了JavaScript實現(xiàn)數(shù)組在指定位置插入若干元素的方法,涉及javascript中splice方法的使用技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04
學(xué)習(xí)javascript的閉包,原型,和匿名函數(shù)之旅
Javascript中有幾個非常重要的語言特性——對象、原型繼承、閉包。其中閉包 對于那些使用傳統(tǒng)靜態(tài)語言C/C++的程序員來說是一個新的語言特性,本文給大家介紹js的閉包,原型,和匿名函數(shù)之旅,感興趣的朋友一起學(xué)習(xí)吧2015-10-10
理解Javascript的caller,callee,call,apply區(qū)別
理解Javascript的caller,callee,call,apply區(qū)別...2007-03-03

