2017-10-22 6 views
-1

チェックボックスをオンにすると、CSSのボディの色を変更したいと思います。どうやってするか?チェックボックスのボディカラーを変更する

HTML -

<label class="switch"> 
    <input type="checkbox"> 
    <span class="slider round"></span> 
</label> 

JavaScriptを - ここに貼り付けることは何?

CSS - ここまでは何ですか?

ありがとうございました!

+0

jQueryを使用していますか? – user2887596

+0

いいえ、私はjQueryを使用していません。 – Miner123

+0

これまでに試したことを含めてください – Nick

答えて

0

[OK]を、あなたは私がコード

HTMLコメントしてきたJSに慣れていないので、:

<body> 
<p> 
    test 
</p> 
<label class="switch"> 
    <input type="checkbox" id="test"> 
    <span class="slider round"></span> 
</label> 

JS:

var checkedColor="red"; //the colors of the background 
var uncheckedColor="blue" //the colors of the background 
document.body.style.backgroundColor =uncheckedColor;//change the color at the begining to keep it consistent 
function changeColor(){ 

    if (document.getElementById("test").checked) { //if it is checked change to checkedColor 
     document.body.style.backgroundColor = checkedColor; 
    } 
    else {//if it is not checked change to uncheckedColor 
    document.body.style.backgroundColor =uncheckedColor; 
    } 
} 


document.getElementById("test").addEventListener('click', changeColor); //make the checkbox exeute the function test when being clicked 

ここでは、実施例です。 https://jsfiddle.net/25t64qha/

+0

それは私を助けました。どうもありがとうございます!!!! – Miner123

関連する問題