html - 将选中的复选框从勾号更改为纯色背景色

标签 html css twitter-bootstrap

默认情况下,当您选中一个复选框时,它会在该框中打勾。我正在尝试将其更改为纯色天气它是 css 背景或检查时的图像。 http://bootply.com/71996

<label class="checkbox">
    <input type="checkbox" value="">
    Content
</label>

我在这里搜索并阅读了一些答案,但没有定论和简单的答案。

最佳答案

your updated code .

HTML:

<label class="checkbox">
<input value="" type="checkbox">
Content
</label>

JavaScript:

$("input[type=checkbox]").on("change", function() {
  var $chk = $(this);
  var isChecked = $chk.prop('checked');
  if (isChecked) {
    $chk.parent().addClass("checked");
  }
  else {
    $chk.parent().removeClass("checked");
  }
});

CSS:

.checkbox { color:blue; }
.checkbox:before { content:""; background-color:blue; width:15px; height:18px; display:block; position:absolute; margin-left:-20px; }
.checkbox.checked { color:red; }
.checkbox.checked:before { background-color:red; }

注意:

JavaScript 可以简化,但我有意在每次更改时检查值(而不是使用 .toggleClass())。

关于html - 将选中的复选框从勾号更改为纯色背景色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18060807/

相关文章:

html - 应用于 :before element of css did not get applied to the entire div 的背景

javascript - 如何在 HTML 页面中注入(inject)个人脚本?

javascript - AJAX 构建和部署环境,尤其是 javascript 和 css min,完整版本

javascript - jQuery "Slide"效果绕过 <br>

带有 Twitter Bootstrap 进度条的 Javascript 倒计时

html - Spring + Thymeleaf + Bootstrap

css - 什么是 Bootstrap 禁用按钮悬停类名称?

javascript - 删除 HTML 元素的内联 css

javascript - createTextNode 中的新行

html - CSS 还是编码?如何使结果并排列出而不是彼此重叠