JS之点击按钮改变背景颜色.md

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>改变颜色</title>
</head>
<body>
    <input type="button" value="点击切换颜色" id="btn">
</body>
<script>
    var btn = document.getElementById("btn");
    var body = document.body;
    let array = ["red", "blue", "yellow", "white", "black", "green", "orange","#FFF"];
    let index = 0;
    btn.onclick = function () {
        body.style.backgroundColor = array[index];
        index = (index + 1) % array.length;
    }

</script>

</html>

 

posted @ 2022-04-10 22:30  好(justice)……  阅读(582)  评论(0编辑  收藏  举报