javascript - 使用 CSS 填充选中的复选框

标签 javascript jquery html css

是否可以在选中时用背景色填充复选框?还要禁用“滴答声”本身。

我如何实现这一点?我应该为此查看 jQuery/Javascript 吗?

fiddle 演示:http://jsfiddle.net/119a7e8r/

li.squaredTwo li label {
  display:none
}

input[type=checkbox]:checked {
  background:red
}
<ul>

  <li id='field_1_20' class='gfield squaredTwo field_sublabel_below field_description_below' >
  
  <label class='gfield_label'  >Is it free</label>
  
  <div class='ginput_container ginput_container_checkbox'>
    
    <ul class='gfield_checkbox' id='input_1_20'>
  
      <li class='gchoice_1_20_1'>
        <input name='input_20.1' type='checkbox'  value='Yes'  id='choice_1_20_1' tabindex='35'  />
        <label for='choice_1_20_1' id='label_1_20_1'>Yes</label>

      </li>
      
    </ul>
    
  </div>
  
  </li>

</ul>
    

最佳答案

这是一个用于自定义复选框的漂亮的纯 CSS 解决方案:

/* hide the default checkbox */
.checkbox {
  display: none;
}

.label {
  cursor: pointer;
}
/* add pseudo-element as fake checkbox before the label */
.checkbox + .label:before {
  width: 10px;
  height: 10px;
  border: 2px solid gray;
  border-radius: 2px;
  box-shadow: -1px 1px 2px 2px rgba(0,0,0,0.2);
  background: red;
  position: relative;
  display: inline-block;
  margin-right: 1ex;
  content: "";
}

/* style the pseudo element for checked box */
.checkbox:checked + .label:before {
  background: green;
}
<input type="checkbox" class="checkbox" id="myinput" />
<label class="label" for="myinput">Click me</label>

关于javascript - 使用 CSS 填充选中的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34295440/

相关文章:

javascript - 如何在单独的 javascript 文件中获取 HiddenField 值 [asp.net]

javascript - 如何使用 Jquery 选择内联文本?

javascript - 在运行时更改 css 关键帧属性

javascript - 显示来自另一个网站的所有图像的客户端脚本

html - 如何在 RTL 模式下对内联元素进行排序?

javascript - 无法使用 ng-Bootstrap 日期选择器

javascript - 将函数添加到 Angular 自定义指令的范围

javascript - 如何在 Javascript 中添加函数 n 个参数?添加(3)(8)(6)(10)

javascript - 在 Windows 8 应用商店应用程序中调整 HTML5 输入控件的大小

html - 使用文本对齐时 div 之间的空间