码迷,mamicode.com
首页 > 其他好文 > 详细

Event.target和Event.currentTarget的区别

时间:2018-09-12 17:07:43      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:default   color   targe   code   点击   veh   prevent   width   att   

 <style>
    * {
        margin:0;
        padding:0;
        list-style:none;
    }
    #ul {
        width:400px;
        height:250px;
        margin:0 auto;
    }
    
  </style>
 </head>
 <body>
    <ul id=‘ul‘ style=‘border:1px solid black‘>UL
        <li>LI<a href=‘‘>a</a></li>
        <li>LI<a href=‘‘>a</a></li>
        <li>LI<a href=‘‘>a</a></li>
        <li>LI<a href=‘‘>a</a></li>
    </ul>
 <script>
    var EventUtil = {
        addHandler : function(element,type,handler){
            if (element.addEventListener)//标准
            {
                element.addEventListener(type,handler,false);
            }else if (element.attachEvent)//老IE
            {    //handler,必须保证this指向调用的对象,因为在IE下指向window
                element.attachEvent(‘on‘+type,handler);
            }else {
                element[‘on‘+type] = handler;
            }
        },
        removeHandler : function(element,type,handler){
            if (element.removeEventListener)
            {
                element.removeEventListener(type,handler,false);
            }else if (element.detachEvent)
            {
                element.detachEvent(‘on‘+type,handler);
            }else {
                element[‘on‘+type] = null;
            }
        },
        init: function(arr){
            for (var i=0;i<arr.length ;i++ )
            {
                arr[i].style.height = 50+‘px‘;
                if (i%2 == 0)
                {
                    arr[i].style.background = ‘red‘;
                }else {
                    arr[i].style.background = ‘blue‘;
                }
            }
        },
        fn: function(event){
            var bar = event.target;//返回真正的点击的元素
            var tar = bar.nodeName.toLowerCase();
            console.log(‘你点击了:‘+tar);
            event.preventDefault();
        }
    },
    oUl = document.getElementById(‘ul‘),
    aLi = oUl.getElementsByTagName(‘li‘);
    EventUtil.init(aLi);
    EventUtil.addHandler(oUl,‘click‘,EventUtil.fn);
 </script>
 </body>

target返回的是真正的点击的元素

 <style>
    * {
        margin:0;
        padding:0;
        list-style:none;
    }
    #ul {
        width:400px;
        height:250px;
        margin:0 auto;
    }
    
  </style>
 </head>
 <body>
    <ul id=‘ul‘ style=‘border:1px solid black‘>UL
        <li>LI<a href=‘‘>a</a></li>
        <li>LI<a href=‘‘>a</a></li>
        <li>LI<a href=‘‘>a</a></li>
        <li>LI<a href=‘‘>a</a></li>
    </ul>
 <script>
    var EventUtil = {
        addHandler : function(element,type,handler){
            if (element.addEventListener)//标准
            {
                element.addEventListener(type,handler,false);
            }else if (element.attachEvent)//老IE
            {    //handler,必须保证this指向调用的对象,因为在IE下指向window
                element.attachEvent(‘on‘+type,handler);
            }else {
                element[‘on‘+type] = handler;
            }
        },
        removeHandler : function(element,type,handler){
            if (element.removeEventListener)
            {
                element.removeEventListener(type,handler,false);
            }else if (element.detachEvent)
            {
                element.detachEvent(‘on‘+type,handler);
            }else {
                element[‘on‘+type] = null;
            }
        },
        init: function(arr){
            for (var i=0;i<arr.length ;i++ )
            {
                arr[i].style.height = 50+‘px‘;
                if (i%2 == 0)
                {
                    arr[i].style.background = ‘red‘;
                }else {
                    arr[i].style.background = ‘blue‘;
                }
            }
        },
        fn: function(event){
            var bar = event.currentTarget;//返回真正的点击的元素
            var tar = bar.nodeName.toLowerCase();
            console.log(‘你点击了:‘+tar);
            event.preventDefault();
        }
    },
    oUl = document.getElementById(‘ul‘),
    aLi = oUl.getElementsByTagName(‘li‘);
    EventUtil.init(aLi);
    EventUtil.addHandler(oUl,‘click‘,EventUtil.fn);
 </script>
 </body>

返回的是绑定的事件的对象和this一样

Event.target和Event.currentTarget的区别

标签:default   color   targe   code   点击   veh   prevent   width   att   

原文地址:https://www.cnblogs.com/jokes/p/9635336.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!