当按钮被点击时,我该如何更改其背景颜色?

9

我做了很多研究,发现这个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个回答

11

这相当简单。只需将 "button" label 元素移动到复选框后面,以便您可以使用 + 选择器选择它(这称为相邻兄弟选择器;不幸的是,没有前一个兄弟选择器),并更改 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>


我认为您可能忘记删除这个选择器 - label + input[type="checkbox"]:checked :) - Harry
2
@Harry 那是 OP 代码的一部分,不会产生干扰,所以我没有删除它。除了最小示例之外,我不知道还发生了什么。 :-) - TylerH
可以理解,但我认为(或者假设)OP试图使用该选择器更改标签的颜色,但由于明显的原因它没有起作用。 - Harry

3
为了实现您想要的效果,在CSS中当按钮被点击时使用background-color
":active"属性用于显示点击事件样式...
HTML:
<input type='button' class='btn' value='test'/>

CSS:

.btn {
    background-color:#00ff00;
}

.btn:active {
    background-color:#ff00ff;
}

TEST


这只会在鼠标悬停时更改按钮颜色,而不会在单击时切换背景颜色。 - TylerH
这将使它在点击时发生变化,但仅在点击时(:active伪类)。我的理解(以及其他回答者)是OP希望它在单击后切换(并保持切换状态)。毕竟,这似乎是上面代码中使用复选框黑客的目的。 - TylerH
3
好的,我明白了。我被“点击时更改颜色”这句话误导了......对我来说,这听起来就像只有在活动状态下才会更改颜色。 - Francesco V.

2
在CSS中,你不能往回走,应该像下面这样向前走。需要在输入框之后放置标签。+用于选择下一个同级元素。没有“上一个同级元素”的选择器。

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>

请查看更新后的Fiddle示例


2

尝试使用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>


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>


1

改变

input[type="checkbox"]:checked + div{
    background: #5BC0DE;
}

input[type="checkbox"]:checked + .btn {
    background: #5BC0DE;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接