###前提・実現したいこと
javascript若しくはjQueryにて下記コードで
チェックボックスにチェックをしたら<div>要素の背景色に
色を薄く塗りたい。
CSSでチェックボックスの画像を変えるところまではいけたのですが、
どうしても背景色が変わりません。
宜しくお願い致します。
###発生している問題・エラーメッセージ
背景色がつかない.htmlだけでみると背景色は
チェックボックスのみについている。
###該当のソースコード(html,css,javascript)
【html】
</head> <body> <div class="chkId"> <label for="c_box1"> <input type="checkbox" id ="c_box1" onclick="chebg('c_box1')"/> <span class="image"></span> <p id="font">チェックボックス</p></label> </div> </body> </html>Myid=document.getElementById(chkID); if(Myid.checked == true){ Myid.parentNode.style.backgroundColor = '#C6E5FF'; }else { Myid.parentNode.style.backgroundColor = '#FFFFFF'; } } </script>
【style】
@charset "utf-8";
/*
html5doctor.com Reset Stylesheet
v1.6.1
Last Updated: 2010-09-17
Author: Richard Clark - http://richclarkdesign.com
Twitter: @rich_clark
*/
html, body, div, span, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, abbr, address, cite, code, del, dfn, em, img, ins, kbd, q, samp, small, strong, sub, sup, var, b, i, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, figcaption, figure, footer, header, hgroup, menu, nav, section, summary, time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
outline: 0;
font-size: 100%;
vertical-align: baseline;
background: transparent;
}
body {
line-height: 1.1em;
}
article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section {
display: block;
}
nav ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after, q:before, q:after {
content: '';
content: none;
}
a {
margin: 0;
padding: 0;
font-size: 100%;
vertical-align: baseline;
background: transparent;
text-decoration: none;
}
change colours to suit your needs ins {
background-color:#ff9;
color:#000;
text-decoration:none;
}
change colours to suit your needs mark {
background-color:#ff9;
color:#000;
font-style:italic;
font-weight:bold;
}
del {
text-decoration: line-through;
}
abbr[title], dfn[title] {
border-bottom: 1px dotted;
cursor: help;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
/* change border colour to suit your needs
/
hr {
display: block;
height: 1px;
border: 0;
border-top: 1px solid #cccccc;
margin: 1em 0;
padding: 0;
}
input, select {
vertical-align: middle;
}
/ --------------------------- reset ここまで ---------------------------- */
section, aiticle, div, header, footer, nav {
display: block;
}
body {
font-size: 100%;
color: black;
font-weight: normal;
font-family: Verdana, "ヒラギノ角ゴ ProN W3", "Hiragino Kaku Gothic ProN", "メイリオ", Meiryo, sans-serif;
vertical-align: baseline;
background: white;
}
.chkId {
width: 600px;
height: 100px;
margin-left: auto;
margin-right: auto;
margin-top: 100px;
border:1px solid #CCC;padding:10px;border-radius:10px;
}
#font {
display: block;
font-size: 18px;
width:145px;
height:20px;
margin-top: 43px;
margin-left: 20px;
float:left;
}
label{
position: relative;
margin-left: 220px;
width:130px;
height:60px;
}
label input{
display: none;
}
label .image{
position: absolute;
left: 0;
top:1.2em;
display: block;
background: url(buttonOff.png) no-repeat 0 0;
width: 130px;
height: 60px;
}
label input:checked + .image{
background:url(buttonOn.png) no-repeat 0 0;
}
###試したこと
Myid.parentNode.style.backgroundColor = '#C6E5FF';
のMyid.parentNode.部分
chiid1=document.getElementById(".chkId");として
chiid1.style.backgroundColor = '#C6E5FF';
としても出来ませんでした。
よろしくお願い致します。
*jQueryでも大丈夫です。お願い致します。
###補足情報(言語/FW/ツール等のバージョンなど)
より詳細な情報
回答2件
あなたの回答
tips
プレビュー