我们知道checkbox类型input复选框在每一种浏览器都有默认样式,那么可不可以自定义样式呢?答案是肯定的,至少在Chrome和FireFox等标准浏览器中可以做到,需要注意的,较新版本Chrome浏览器需要使用-webkit-appearance:none;属性先解除默认样式。以下是代码:全选
input[type='checkbox']{
width: 20px;
height: 20px;
background-color: #fff;
-webkit-appearance:none;
border: 1px solid #c9c9c9;
border-radius: 2px;
outline: none;
}
.checkBox input[type=checkbox]:checked{
background: url("../images/checkbox.png") no-repeat center;
}
如果需要考虑兼容IE等浏览器,则可以用div + js方式模拟复选框达到美化目的,以下是参考代码:
.agreement-select { background-image: url("./true.png");
}.agreement-no-select { background-image: url("./false.png");
}.agreement-ack { box-sizing: border-box; width: 30px; height: 30px; background-size: 30px 30px;
}
var js_ack = document.getElementsByClassName("js-ack")[0];
js_ack.onclick = function () { if (js_ack.className.indexOf("agreement-select") > -1) {
js_ack.classList.remove("agreement-select");
js_ack.classList.add("agreement-no-select");
} else {
js_ack.classList.remove("agreement-no-select");
js_ack.classList.add("agreement-select");
}
};