prop()和attr()都可以设置元素中的属性。
但是attr()方法在设置属性的时候,最好设置固有元素,不适合设置只有true和false值得返回属性
attr(“attribute name”,“attribute value”) or attr(“attribute name”)方法
<input class="" id="" type="">
$("selector").attr("id","xxx");
$("selector").attr("class","xxx");
$("selector").attr("herf","xxx");
$("selector").attr("herf","xxx");
prop(“attribute name”) or prop(“attribute name”,“true/false”)##
如果在访问表单元素时候,遇到disable,或者是checked时候,还有selected的时候,使用attr()设置为true/false的时候,可能会遇到bug,所以这里对于这种返回值只有boolean类型的元素,推荐使用prop()方法,进行设置,如果要获取对应属性的返回值,只需传入元素名字即可
$("selector").prop("disable") //返回true或者是false
$("selector").prop("disable","true"); //设置disable
prop也可以根据别人的状态来更改其他元素的状态
取得全选框的选中状态,在调用其他框的对象,调用prop设置他的checked为全选框的状态
removeAttr(“attribute name”)
从每一个匹配的元素中删除一个属性
1.6以下版本在IE6使用JQuery的removeAttr方法删除disabled是无效的。解决的方法就是使用$(“XX”).prop(“disabled”,false);
1.7版本在IE6下已支持删除disabled。
$("this").removeAttr("readonly");
$(this).removeAttr("disable");
//删除图片中的 src的属性
$("img").removeAttr("src");