zoukankan      html  css  js  c++  java
  • 事件委托能够优化js性能

    <!DOCTYPE html>
    
    <html>
    
     
    
    <head>
    
    <meta charset="UTF-8">
    
    <title>面试题练习img居中</title>
    
    <style type="text/css">
    
    img {
    
    /*display: table-cell;
    
    vertical-align: middle;*/
    
    }
    
    html,body{
    
    height: 100%;
    
    }
    
    div{
    
    width: 600px;
    
    height: 1600px;
    
    display: table-cell;
    
    vertical-align: middle;
    
    text-align: center;
    
    }
    
    </style>
    
    </head>
    
     
    
    <body>
    
    <ul>
    
    <li id="a">1</li>
    
    <li id="b">2</li>
    
    <li id="c">3</li>
    
    <li id="d">4</li>
    
    <li id="e">5</li>
    
    </ul>
    
    </body>
    
    <script type="text/javascript">
    
    var str = document.getElementsByTagName("ul")[0];
    
     
    
    str.onclick = function(){
    
    var hehe = event || window.event;
    
    var target = hehe.target || hehe.srcElement;
    
     
    
    switch(target.id){
    
    case "a":{
    
    console.log(1);
    
    this.style.background = "orange";
    
    break;
    
    }
    
    case "b":{
    
    console.log(2);
    
    this.style.background = "orange";
    
    break;
    
    }
    
    case "c":{
    
    console.log(3);
    
    this.style.background = "orange";
    
    break;
    
    }
    
    case "d":{
    
    console.log(4);
    
    this.style.background = "orange";
    
    break;
    
    }
    
    case "e":{
    
    console.log(5);
    
    this.style.background = "orange";
    
    break;
    
    }
    
    }
    
    }
    
    </script>
    
    </html>
    
    使用事件委托以后,可以不必在每个子元素上绑定事件,能够优化性能,同时对于一些动态添加进去的子元素,事件委托也要比执行回调函数绑定事件更加方便
  • 相关阅读:
    解决粘包问题
    粘包问题
    模拟ssh功能
    套接字接上链接循环
    套接字加入通讯循环
    简单通信
    网络编程
    单例模式
    记录一下:chrome上,把网页保存为文件的插件
    centos6.5 64bit 实现root开机自动登录X桌面
  • 原文地址:https://www.cnblogs.com/yk-ontheway/p/5827800.html
Copyright © 2011-2022 走看看