JS点击变色小案例

2020-11-15  本文已影响0人  白蓝青黄紫

版本一(行内式写法)

css部分

html,body {
        width: 100%;
        height: 100%;
        background-color:wheat;
}

html部分

<input type="button" value="点我变红" onclick="document.body.style.background='red'">

<input type="button" value="点我变蓝" onclick="document.body.style.background='blue'">

<input type="button" value="点我变黄" onclick="document.body.style.background='yellow'">

<input type="button" value="点我变绿" onclick="document.body.style.background='green'">

<input type="button" value="点我变白" onclick="document.body.style.background='#fff'">

<input type="button" value="点我变黑" onclick="document.body.style.background='rgb(0,0,0)'">

注释内容

版本二(函数式写法)

css部分

html,body {
        width: 100%;
        height: 100%;
        background-color:wheat;
}

HTML部分

 <button id="redBtn">红色</button>

JS部分

var redBtn = document.getElementById("redBtn").onclick = function(){
            document.body.style.background = "red"
        }

var redBtn = document.getElementById("redBtn");
redBtn. onclick = function(){
  document.body.style.background = "red"
}
注释内容
上一篇 下一篇

猜你喜欢

热点阅读