【JS】无他按钮+className属性

Osinghong4周前 (05-19)成果展示49
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>d24c04429</title>
    <style>
        .target {
            width: 100px;
            height: 100px;
            font-size: 20px;
            text-align: center;
            line-height: 100px;
        }
    </style>
</head>
<body>
    <div class="box">brat</div><br>
    <button>yellow</button>
    <button>none1</button>
    <button>none2</button>
    <button>none3</button>
    <button>none4</button>
    <script>
        var box = document.querySelector('.box');
        box.className = 'target';
        var btns = document.querySelectorAll('button');
        for(var i = 0; i < btns.length; i++) {
            btns[i].onclick = function() {
                if(this === btns[0]) {
                    box.style.backgroundColor = 'yellow';
                } else {
                    box.style.backgroundColor = '';
                }
            }
        }
    </script>
</body>
</html>


打赏
分享给朋友:

相关文章