我们知道各个浏览器的复选框(checkbox)的样式不统一,IE下的更是很丑,下面我们来模拟复选框(checkbox)
思路:把默认的checkbox表单隐藏起来,旁边增加一个标签替代,对标签进行点击操作的同时,改变checkbox的选中状态。
效果图:
HTML CODE
[html]
选项一
选项二
[/html]
CSS CODE
[css].checkbox{width: 15px;height: 15px;display: block;float: none;border:1px solid #DBDBDB;background: #F5F7F9;cursor: pointer;position: absolute;top: 0;left: 0;}
.checkbox-con .cur{border:none;width: 17px;height: 17px;background:url(https://www.w3cways.com/wp-content/uploads/2014/07/1.png) no-repeat;}
.checkbox-con span{display: inline-block;position: relative;padding-left: 20px;margin-right: 10px;}
.checkbox-con .ipt-hide{position: absolute;width: 0;height: 0;border: none;}
[/css]
jQuery CODE
先引入jquery文件
[html]
[/html]
接着写JS
[js]$(function () {
$(‘.checkbox’).on(‘click’,function(){
if($(this).siblings("input[type=’checkbox’]").attr(‘checked’)){
$(this).removeClass(‘cur’);
$(this).siblings("input[type=’checkbox’]").removeAttr(‘checked’)
}
else{
$(this).addClass(‘cur’);
$(this).siblings("input[type=’checkbox’]").attr(‘checked’,’checked’)
}
});
});
[/js]
该博客介绍了一种解决浏览器间复选框样式不一致问题的方法,通过隐藏默认的checkbox,用CSS和JavaScript创建自定义样式。点击标签可以改变checkbox的选中状态,实现了视觉效果的一致性。

被折叠的 条评论
为什么被折叠?



