(上図解説)
左4個は押しても変化がない、
右9個は押すと(左から6番目のように)黒くなり、
再び押すと白に戻る。
この色の変わるボタンの色をリセットし、白に戻すリセットボタンを作りたい。
これ以外にも作っているサイトにはリセットボタンを作っているので、
「bodyの項目すべてをリセット」ではなくここだけをリセットしたい。
ご依頼したいのは
・リセットボタンに関する部分のコード(主にスクリプトコード)
・その解説
です。
自分で書くとリセットボタンが反応せず、完全に手詰まって住まいました。
ネットで調べて色々くっつけて作ったものなので、他のコードのおかしなところも
ぜひ教えてください。
以下のHEMLコードは関係ありそうなところだけを抜き出し注釈を入れたものです
<head> <script type="text/javascript"> function func_test(obj) { if (obj.style.color == 'white') { obj.style.color = 'black'; obj.style.background = 'white'; } else { obj.style.color = 'white'; obj.style.background = 'black'; } } </script> <style type="text/css"> <!-- .item { width: 30px; height: 40px; border: 1px solid #ccc; background-color: white; color: black; border-bottom: 5px solid black; } .noitem { width: 30px; height: 40px; border: 1px solid #ccc; background-color: white; border-bottom: 5px solid #ccc; } .noitem1 { width: 30px; height: 40px; border: 1px solid #ccc; background-color: #ccc; border-bottom: 5px solid #ccc; } --> </style> </head> <body> <script type="text/javascript"> //教えていただきたいリセットボタンのコード function OnButtonClick(obj) { document.getElementsByClassName("item").style.color = 'black'; document.getElementsByClassName("item").style.background = 'white'; } </script> <input type="button" id="button_sample" value="リセット" onclick="OnButtonClick(this)" /> <label> <div class="click-button"> <button class="noitem"> </button> //ここの色は変化させたくない <button class="noitem"> </button> //ここの色は変化させたくない <button class="noitem"> </button> //ここの色は変化させたくない <button class="noitem1"> </button> //ここの色は変化させたくない <button class="item" onclick="func_test(this)"> </button> <button class="item" onclick="func_test(this)"> </button> <button class="item" onclick="func_test(this)"> </button> <button class="item" onclick="func_test(this)"> </button> <button class="item" onclick="func_test(this)"> </button> <button class="item" onclick="func_test(this)"> </button> <button class="item" onclick="func_test(this)"> </button> <button class="item" onclick="func_test(this)"> </button> <button class="item" onclick="func_test(this)"> </button> </div> </label> </body>
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/05/18 08:55