思路:使用label上的for熟悉,与checkbox上的id相对应来达到点击选中效果,在使用伪元素,或者其余元素,定位至checkbox上方,替代checkbox,而且隐藏checkbox,使用CSS3选择器:checked + 毗邻选择器,选中后给伪元素,或者其余元素添加选中样式;css
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> .label1 { position: relative; } .label1:after { position: absolute; top: 0px; left: -22px; width: 16px; height: 16px; background: #ededed; border: 1px solid #dcdcdc; content: ''; } form input[type='checkbox']:checked + .label1:after { background: red; } </style> </head> <body> <form action=""> <input type="checkbox" name="a" id="a1" onchange="check()"> <label class="label1" for="a1">记住密码</label> </form> </body> </html>
.label1 {
position: relative;
color: red\0; /*IE8+都能使用这条样式,这行需写在IE7hack之上,在win10中测试的时候发现,IE7执行了该行样式,可是数值为空*/
+color: red; /*IE7才能使用这条样式*/
}
form:nth-child(1) .label1 { /* IE9+才能使用的样式*/
color: green;
}
结构参考上面的代码