【问题标题】:How can I change the background color of a button when clicked?单击时如何更改按钮的背景颜色?
【发布时间】:2015-08-08 23:27:53
【问题描述】:

我研究了很多,我发现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>

【问题讨论】:

    标签: javascript html css checkbox


    【解决方案1】:

    改变

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

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

    【讨论】:

      【解决方案2】:

      .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>

      【讨论】:

        【解决方案3】:

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

        ":active" 属性用于显示点击事件样式...

        HTML:

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

        CSS:

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

        TEST

        【讨论】:

        • 这只会改变悬停时的按钮颜色;它不会在点击时切换背景颜色。
        • 将其更改为单击,但仅在单击时(:active 伪类)。我的解释(和其他回答者)是 OP 希望它在单击后切换(并保持切换)。毕竟,这似乎是上面代码中使用的复选框 hack 的目的。
        • 好的,我明白了。我被“点击时改变颜色”误导了……对我来说,这听起来像是只在活动状态下。
        【解决方案4】:

        这是相当微不足道的。只需将“按钮”label 元素移动到复选框之后,以便您可以使用+ 选择器选择它(这称为adjacent sibling selector;不幸的是,没有先前的同级选择器),并更改 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 那是 OP 代码的一部分并且没有干扰,所以我没有删除它。我不知道除了最小示例之外还发生了什么。 :-)
        • 很公平,但我认为(或假设)OP 尝试使用该选择器更改标签的颜色,但没有成功(原因很明显)。
        【解决方案5】:

        在 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

        【讨论】:

          【解决方案6】:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-10-09
            • 2017-08-02
            • 1970-01-01
            • 1970-01-01
            • 2014-10-10
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多