为非IE浏览器添加mouseenter,mouseleave事件

先了解几个事件对象属性

target: 指事件源对象,点击嵌套元素最里层的某元素,该元素就是target。IE6/7/8对应的是srcElement。
currentTarget: 指添加事件handler的元素本身,如el.addEventListener中el就是currentTarget。IE6/7/8没有对应属性,可在handler内使用this来替代如evt.currentTarget = this。
relativeTarget: 指事件相关的元素,一般用在mouseover,mouseout事件中。IE6/7/8中对应的是fromElement,toElement。

mouseentermouseleave IE9中仍然支持,mouseenter与mouseover区别在于:在元素内部移动时mouseenter不会触发。如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <title>mouseerter与mouseover区别(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,鼠标在div[id=parent1]内部移动时会触发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,鼠标在div[id=parent2]内部移动时不会触发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>

  

  

了解了这三个属性的意义后,实现起来就很简单

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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};
}();

  

 

相关:

Greg Reimer 的博文 Goodbye mouseover, hello mouseenter

posted on   snandy  阅读(4144)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端
< 2011年3月 >
27 28 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31 1 2
3 4 5 6 7 8 9

统计

点击右上角即可分享
微信分享提示