Jquery中attr()与prop()方法的差异

1.prop(propertyName)                                                                                                 Returns:String  or  Boolean

官方描述:Get the value of a property for the first element in the set of matched elements.(获取匹配元素集合中第一个相匹配元素的值)

如果the value of protopy 没有被设置,则返回undefined.

注意:在IE6,7,8中试图通过此方法来改变通过HTML或者已经存在于HTML document中的input的元素的属性将会报错。

2.Attributes 与Properties 区别

在Jquery1.6之前,还没有prop()方法,我们通过attr()可以获取元素的属性值,但是这样会产生一些问题;

比如官方例子:

<input type="checkbox" checked="checked" />
elem.checked                               //return true(Boolean)
elem.getAttribute("checked")               //return "checked"
$(elem).attr("checked")                    //(1.6)return "checked"
$(elem).attr("checked")                    //(1.6.1+)return "checked"
$(elem).attr("checked")                    //(pre-1.6)return true

Jquery1.6之后呢,出现了porp()这个方法

  

$(element).prop("checked")           //return true

总结:就是说,对于selectedIndextagNamenodeNamenodeTypeownerDocumentdefaultChecked, and defaultSelected元素,我们需要使用prop方法还获取其是boolean值,对于attr方法,我们修改的是其普通属性。在360急速模式下,使用attr("selected")将会产生错误,但调试器不会报错。此错误在IE10,chrome,firefox,opera中都不存在。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值