I researched a lot, and I found this JSFiddle looks like what I need, but, when I click the button, the page background is changing. I want the button's color to be changed when clicked, instead.
我研究了很多,我发现这个JSFiddle看起来就是我需要的,但是当我点击按钮时,页面的背景就会发生变化。我希望按钮的颜色在点击时被改变。
div{
width: 100%;
height: 100%;
background: #5CB85C;
position: absolute;
top: 0;
left: 0;
z-index:1;
}
label.btn {
position: absolute;
top:10px;
left: 10px;
z-index:2;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
input[type="checkbox"]{
display: none;
}
input[type="checkbox"]:checked + div{
background: #5BC0DE;
}
label + input[type="checkbox"]:checked{
background: #000;
}
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet"/>
<label for="check" class="btn btn-default">Toggle background colour</label>
<input type="checkbox" id="check" />
<div></div>
6 个解决方案
#1
12
This is fairly trivial. Simply move the "button" label
element to after the checkbox so that you can select it with the +
selector (this is called the adjacent sibling selector; there is no previous sibling selector, unfortunately), and change the CSS to match. JSFiddle
这是相当简单的。只需将“按钮”标签元素移动到复选框之后,以便您可以使用+选择器选择它(这称为相邻的兄弟选择器;不幸的是,之前没有兄弟选择器),并将CSS更改为匹配。JSFiddle
div{
width: 100%;
height: 100%;
background: #5CB85C;
position: absolute;
top: 0;
left: 0;
z-index:1;
}
.btn {
position: absolute;
top:10px;
left: 10px;
z-index:2;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
input[type="checkbox"]{
display: none;
}
input[type="checkbox"]:checked + .btn {
background: #5BC0DE;
}
/* label + input[type="checkbox"]:checked{
background: #000;
} */ /* I've commented this last selector out as it is unused per Harry's point in the comments */
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet"/>
<input type="checkbox" id="check" />
<label for="check" class="btn btn-default">Toggle background color</label>
<div></div>
#2
3
To achieve what you want, in your css use background-color
when the button is clicked.
要实现您想要的效果,在css中单击按钮时使用背景色。
":active" property is used for displaying clicked event style...
“:active”属性用于显示单击事件样式……
HTML:
HTML:
<input type='button' class='btn' value='test'/>
CSS:
CSS:
.btn {
background-color:#00ff00;
}
.btn:active {
background-color:#ff00ff;
}
测试
#3
2
Try using css pseudo code:
尝试使用css伪代码:
#check {
display: none;
}
#check + label.btn {
cursor: pointer;
border: 1px solid grey;
padding: 6px 8px;
background: ghostwhite;
border-radius: 7px;
box-shadow: 0px 1px 1px;
}
#check:checked + label.btn {
background: wheat;
}
#check + label.btn:hover {
box-shadow: 0px 3px 2px;
}
#check + label.btn:active {
box-shadow: 0px 1px 1px inset;
}
<input type="checkbox" id="check" />
<label for="check" class="btn">Toggle background colour</label>
#4
2
In css you can not go backward you should go forward like following.
You need to put label after input box.
在css中,你不能后退,你应该像下面这样向前走。你需要在输入框后加上标签。
+
is for the next sibling. There is no "previous sibling" selector.
+代表下一个兄弟姐妹。没有“先前的兄弟”选择器。
div{
width: 100%;
height: 100%;
background: #5CB85C;
position: absolute;
top: 0;
left: 0;
z-index:1;
}
label.btn {
position: absolute;
top:10px;
left: 10px;
z-index:2;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
input[type="checkbox"]{
display: none;
}
input[type="checkbox"]:checked + label{
background: #5BC0DE;
}
label + input[type="checkbox"]:checked{
background: #000;
}
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet"/>
<input type="checkbox" id="check" />
<label for="check" class="btn btn-default">Toggle background colour</label>
<div></div>
Check Updated Fiddle
检查更新的小提琴
#5
1
.nl [type=checkbox]:checked + span {
background: #222222;
background-repeat: no-repeat;
}
.nl label span {
height: 18px;
width: 18px;
float: left;
margin-right: 9px;
border: 1px solid #d5d5d5;
display: inline-block;
background-color: #ff0000;
}
input[type="checkbox"], .checkbox-inline input[type="checkbox"] {
position: absolute;
margin-top: 4px \9;
margin-left: -20px;
}
.nl label input {
display: none;
}
<div class="checkbox nl">
<label><input type="checkbox" class="checkboxx" name="rememberme" id="rememberme" value="1" checked="checkced"> Remember me<span></span></label>
<input type="hidden" name="action" value="login">
</div>
#6
1
change
改变
input[type="checkbox"]:checked + div{
background: #5BC0DE;
}
to
来
input[type="checkbox"]:checked + .btn {
background: #5BC0DE;
}
#1
12
This is fairly trivial. Simply move the "button" label
element to after the checkbox so that you can select it with the +
selector (this is called the adjacent sibling selector; there is no previous sibling selector, unfortunately), and change the CSS to match. JSFiddle
这是相当简单的。只需将“按钮”标签元素移动到复选框之后,以便您可以使用+选择器选择它(这称为相邻的兄弟选择器;不幸的是,之前没有兄弟选择器),并将CSS更改为匹配。JSFiddle
div{
width: 100%;
height: 100%;
background: #5CB85C;
position: absolute;
top: 0;
left: 0;
z-index:1;
}
.btn {
position: absolute;
top:10px;
left: 10px;
z-index:2;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
input[type="checkbox"]{
display: none;
}
input[type="checkbox"]:checked + .btn {
background: #5BC0DE;
}
/* label + input[type="checkbox"]:checked{
background: #000;
} */ /* I've commented this last selector out as it is unused per Harry's point in the comments */
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet"/>
<input type="checkbox" id="check" />
<label for="check" class="btn btn-default">Toggle background color</label>
<div></div>
#2
3
To achieve what you want, in your css use background-color
when the button is clicked.
要实现您想要的效果,在css中单击按钮时使用背景色。
":active" property is used for displaying clicked event style...
“:active”属性用于显示单击事件样式……
HTML:
HTML:
<input type='button' class='btn' value='test'/>
CSS:
CSS:
.btn {
background-color:#00ff00;
}
.btn:active {
background-color:#ff00ff;
}
测试
#3
2
Try using css pseudo code:
尝试使用css伪代码:
#check {
display: none;
}
#check + label.btn {
cursor: pointer;
border: 1px solid grey;
padding: 6px 8px;
background: ghostwhite;
border-radius: 7px;
box-shadow: 0px 1px 1px;
}
#check:checked + label.btn {
background: wheat;
}
#check + label.btn:hover {
box-shadow: 0px 3px 2px;
}
#check + label.btn:active {
box-shadow: 0px 1px 1px inset;
}
<input type="checkbox" id="check" />
<label for="check" class="btn">Toggle background colour</label>
#4
2
In css you can not go backward you should go forward like following.
You need to put label after input box.
在css中,你不能后退,你应该像下面这样向前走。你需要在输入框后加上标签。
+
is for the next sibling. There is no "previous sibling" selector.
+代表下一个兄弟姐妹。没有“先前的兄弟”选择器。
div{
width: 100%;
height: 100%;
background: #5CB85C;
position: absolute;
top: 0;
left: 0;
z-index:1;
}
label.btn {
position: absolute;
top:10px;
left: 10px;
z-index:2;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
input[type="checkbox"]{
display: none;
}
input[type="checkbox"]:checked + label{
background: #5BC0DE;
}
label + input[type="checkbox"]:checked{
background: #000;
}
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet"/>
<input type="checkbox" id="check" />
<label for="check" class="btn btn-default">Toggle background colour</label>
<div></div>
Check Updated Fiddle
检查更新的小提琴
#5
1
.nl [type=checkbox]:checked + span {
background: #222222;
background-repeat: no-repeat;
}
.nl label span {
height: 18px;
width: 18px;
float: left;
margin-right: 9px;
border: 1px solid #d5d5d5;
display: inline-block;
background-color: #ff0000;
}
input[type="checkbox"], .checkbox-inline input[type="checkbox"] {
position: absolute;
margin-top: 4px \9;
margin-left: -20px;
}
.nl label input {
display: none;
}
<div class="checkbox nl">
<label><input type="checkbox" class="checkboxx" name="rememberme" id="rememberme" value="1" checked="checkced"> Remember me<span></span></label>
<input type="hidden" name="action" value="login">
</div>
#6
1
change
改变
input[type="checkbox"]:checked + div{
background: #5BC0DE;
}
to
来
input[type="checkbox"]:checked + .btn {
background: #5BC0DE;
}