黑马 京东关闭广告 作业2 HTML+CSS+JS

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            position: relative;
        }
        img {
            width: 100%;
            height: 200px;
        }
        div a {
            text-decoration: none;
            position: absolute;
            top: 10px;
            right: 10px;
            background-color: rgba(0, 0, 0, 0.2);
            width: 20px;
            height: 20px;
            text-align: center;
            line-height: 20px;
            color: white;
        }
    </style>
    </style>
</head>
<body>
    <div>
        <img src="img/广告.jpg" title="广告">
        <a href="#">×</a>
        <script>
            // 1.获取元素
            var cross = document.querySelector('a');
            var img = document.querySelector('img');
            // 2.注册事件 点击关闭
            cross.onclick = function() {
                img.style.display = 'none';
                this.style.display = 'none';
            }
        </script>
        </script>
    </div>
</body>
</html>

你可能感兴趣的:(前端,javascript,html,css)