废话不多说,先上代码:
$('#page-wrapper input[type="checkbox"]').bind('click',function(){
if($(this).is(':checked') == true) {
$(this).parent().find('.one').attr('checked', true);
return true;
}else{
if($(this).parent().find('.two:checked').length == 0){
$(this).parent().find('.one').attr('checked',false);
}
return true;
}
});代码原本的逻辑,是因为权限管理,选择子菜单,主菜单自动选中。子菜单全部取消了,那么主菜单也就自动取消了。
但是不知道什么原因代码不生效,作为一个长期从事前端工作者来说,自然想到的是jquery版本问题。果然项目采用的jquery-2.1.3版本,作为资深前端,你应该知道jquery最稳定的两个版本莫过于1.6.2和1.8.3两个版本,于是乎,换成1.8.3成功了。
那么主管说了,就用jquery-2.1.3不然怕项目之前的东西报错,如果两个都引用是不是影响网页性能呢?作为前端我们深知,3s是网页的一个界定值!那么我们需要不断的优化网页性能,那么小编就想啊。怎么兼容呢?于是乎,就有这样一段代码的出现!
$('#page-wrapper input[type="checkbox"]').bind('click',function(){
if($(this).is(':checked') == true) {
$(this).parent().find('.one').prop('checked', true);
return true;
}else{
if($(this).parent().find('.two:checked').length == 0){
$(this).parent().find('.one').removeAttr('checked',false);
}
return true;
}
});这两个属性的搭配!