小项目(冒泡事件的使用)

<!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>小项目(冒泡事件的使用)</title>
    <style>
        *{margin: 0; padding: 0;}
        #div{
            width: 100%;
            height: 400px;
            border: 1px solid black;
        }
        li{
            width: 60px;
            height: 20px;
            border-radius: 10px/10px;
            text-align: center;
            float: left;
            margin-left: 5px;
            list-style: none;
            cursor: default;
            }
        #ul{margin: 0;}
        #ul>li:first-child{
            background: red;
        }
        #ul>li:first-child+li{
            background: green;
        }
        #ul>li:last-child{
            background: blue;
        }
    </style>
</head>
<body>
    <div id="div"></div>
    <ul id="ul">
        <li>红色</li>
        <li>绿色</li>
        <li>蓝色</li>
    </ul>
    <script>
        var div=document.getElementById("div");//获取div
        var ul=document.getElementById("ul");//获取ul
        //给ul绑定点击事件
        ul.addEventListener("click",function(){
            var li=event.target;//将事件源赋值给变量li
            //判断li的内容
            if (li.innerHTML=="红色"){
                div.style.background="red";
            }else if (li.innerHTML=="绿色"){
                div.style.background="green";
            }else{
                div.style.background="blue";
            }
        })
    </script>
</body>
</html>
原文地址:https://www.cnblogs.com/vinson-blog/p/12115147.html