zhengwei1949 / myblog

个人博客
10 stars 6 forks source link

乐淘总结第三天 #132

Open zhengwei1949 opened 6 years ago

zhengwei1949 commented 6 years ago

总结

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
    </ul>
    <script>
    // var list = document.querySelectorAll('ul>li');
    // for(var i=0;i<list.length;i++){
    //     list[i].onclick = function(){
    //         console.log(this.innerHTML);
    //     }
    // }
    document.querySelector('ul').onclick = function(event){
        // console.log(event);
        if(event.srcElement.tagName == 'LI'){
            // console.log('点击了li标签')
            // console.log(event.srcElement)
            console.log(event.srcElement.innerHTML);
        }
    }
    </script>
</body>
</html>
+ 优点
    1. 快,性能更高
+ 适用场景
    1. 有些元素在刚开始的时候是不存在的,我们这个时候就需要给它绑定事件
+ ajax的执行时机(同步、异步代码执行流程)