2

当我单击其中的复选框时,我想在下面的代码中设置 div#main 的样式。我可以只使用 css/css3 来做到这一点吗?先感谢您!

<div id="main">
<div id="content">
<input type="checkbox"/>
</div>
</div> 
4

4 回答 4

2

你可以玩 HTML & css。像这样写:

<input type="checkbox" id="change"/>
<div id="main">
<div id="content">
<label for="change"></label>
    <h2>hello</h2>
</div>
</div> 

CSS

#main{
  width:100px;
  height:100px;
  background:red;
  text-align:center;
}
input{
    display:none;
}
input:checked ~ #main{
    border:2px solid green;
    font-size:24px;
    background:yellow;
}
input:checked ~ #main label{
    background:url(http://farm6.static.flickr.com/5182/5840005274_b3bcc52bb1_o.png);
}


label{
    background:url(http://farm3.static.flickr.com/2793/5839457953_1690178a65_o.png);
    display:block;
    width:18px;
    height:18px;
}

检查这个http://jsfiddle.net/j5nTx/

于 2012-10-29T10:52:05.093 回答
0

html

<input type="checkbox" class="checkBox"/>

CSS

.checkBox:checked
{
/* apply any css */
border:solid 1px;
}
于 2012-10-29T10:24:11.040 回答
0

使用此代码:

#main #content input[type="checkbox"]:checked {
    /*your styles*/
}

​如果你想要风格#main #content你应该使用jquery:

$(function () {
            $("#chk").change(function () {
                if ($(this).is(":checked"))
                    $("#content").addClass("newClass");
                else {
                    $("#content").removeClass("newClass");
                }
            });
        });

CSS:

.newClass{
 width: 100px;
 height: 100px;
 background-color: red;
}
于 2012-10-29T10:28:40.630 回答
0

用这个

input[type="checkbox"]:checked #content {
    /*your color changing code*/
}

它将样式代码应用于#content何时 input[type="checkbox"]检查,即您想要什么

于 2012-10-29T10:41:30.020 回答