单击时如何更改按钮的背景颜色?

时间:2015-05-27 04:03:16

标签: javascript html css checkbox

我研究了很多,我发现this 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>
&#13;
&#13;
&#13;

6 个答案:

答案 0 :(得分:12)

这是相当微不足道的。只需移动&#34;按钮&#34; 复选框后label元素,以便您可以使用+选择器选择它(这称为adjacent sibling selector;不幸的是,之前没有兄弟选择器),并更改CSS以匹配。的 JSFiddle

&#13;
&#13;
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 */
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:3)

要实现您想要的效果,请在单击按钮时使用background-color

&#34;:活性&#34; property用于显示单击的事件样式...

HTML:

<input type='button' class='btn' value='test'/>

CSS:

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

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

TEST

答案 2 :(得分: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>

检查Updated Fiddle

答案 3 :(得分:2)

尝试使用css伪代码:

&#13;
&#13;
#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;
}
&#13;
<input type="checkbox" id="check" />
<label for="check" class="btn">Toggle background colour</label>
&#13;
&#13;
&#13;

答案 4 :(得分:1)

&#13;
&#13;
.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;
}
&#13;
<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>
&#13;
&#13;
&#13;

答案 5 :(得分:1)

变化

    from socket import *
    import os
    serverSocket = socket(AF_INET, SOCK_STREAM)     #create a socket

    #Prepare server socket
    serverPort = 7000
    serverSocket.bind(('',serverPort))
    serverSocket.listen(1)

    while True:
        print 'Ready to serve . . .'
        connectionSocket, addr = serverSocket.accept()  #create socket for client
        try:
            message =connectionSocket.recv(1024)   #receive message from client
            print message
            filename = message.split()[1]
            f = open(filename[1:])
            outputdata =f.read()                
            #Send contents of the requested file to the client
            for i in range(0, len(outputdata)):
                connectionSocket.send(outputdata[i])
            connectionSocket.close()
            print 'File Received'
        except IOError:
            connectionSocket.send('\n404 File Not Found\n')
            connectionSocket.close()
    serverSocket.close()

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