jquery之prop与attr区别。

一切看下面代码示例
<!DOCTYPE html> <html> <head> <title>全选和反选</title> <script type="text/javascript" src="./jquery-1.11.2.min.js"></script> </head> <body> <input type="checkbox" name="chkFirst"><br/> <input type="checkbox" name="chkSec"> <input type="checkbox" name="chkSec"> <input type="checkbox" name="chkSec"> <input type="checkbox" name="chkSec"> <input type="checkbox" name="chkSec"> <input type="checkbox" name="chkSec"> <input type="checkbox" name="chkSec" > </body> <script type="text/javascript"> var chkFirst = $('input[name="chkFirst"]'); var chkSec = $('input[name="chkSec"]'); chkFirst.click(function(){ // 这里的prop代替attr是因为prop可循环执行,而attr只能执行一次 // is函数判断对象属性,反坏true和false chkSec.prop("checked", $(this).is(":checked")); }); var len = chkSec.length; chkSec.click(function(){ // 在这里也不能用attr chkFirst.prop("checked", $('input[name="chkSec"]:checked').length == len?true:false); }); </script> </html>
原文地址:https://www.cnblogs.com/qqblog/p/6659308.html