jquery 1.6 版本 之前 调用 $('#checkbox').attr('checked') 会返回 true 或者 false 值
而 在 jquery 1.6 版本 之后 调用 $('#checkbox').attr('checked') 会返回 checked 而值不存在时会返回 undefined
在高版本后 jquery 把属性分为两种 一直是属性、另一种是特性。
它将“属性”与“特性”做了区别,属性指的是“name,id”等等,特性指的是“selectedIndex, tagName, nodeName”等等。
JQ1.6之后,可以通过attr方法去获得属性,通过prop方法去获得特性
$("#cb").attr("tagName"); //undefined
$("#cb").prop("tagName"); //INPUT
分享一段全选操作JS
jQuery.fn.extend({
selectAll : function(attrName,attrValue) {
//获取jquery版本
var jquery_ver = $.fn.jquery;
var obj =$(this);
if(parseFloat(jquery_ver)>=1.6){
$(this).click(function() {
if($(obj).prop("checked")) {
$("input[type=checkbox][" + attrName + "='" + attrValue + "']").prop("checked","checked");
} else {
$("input[type=checkbox][" + attrName + "='" + attrValue + "']").prop("checked",null);
}
});
}else{
$(this).click(function() {
if($(obj).attr("checked")) {
$("input[type=checkbox][" + attrName + "='" + attrValue + "']").attr("checked",true);
} else {
$("input[type=checkbox][" + attrName + "='" + attrValue + "']").attr("checked",false);
}
});
}
//将被绑定的checkebox绑定检查事件
$("input[type=checkbox][" + attrName + "='" + attrValue + "']").click(function () {
var allChecked = $("input:checked[" + attrName + "='" + attrValue + "']");
if(parseFloat(jquery_ver)>=1.6){
if ($(obj).prop("checked") === true ) {
$(obj).prop("checked",null);
}
if ( $("input:checkbox[" + attrName + "='" + attrValue + "']").length == allChecked.length ) {
$(obj).prop("checked","checked");
}
}else{
if ($(obj).attr("checked") === true ) {
$(obj).attr("checked",false);
}
if ( $("input:checkbox[" + attrName + "='" + attrValue + "']").length == allChecked.length ) {
$(obj).attr("checked",true);
}
}
});
}
});
调用方法
$("#obj").selectAll("attr","attr");