$('#checkbox').attr('checked') 返回ture和checked 的原因

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");

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值