jquery中prop()方法和attr()方法的区别浅析
2013-09-06 14:39
816 查看
jquery1.6中新加了一个方法prop(),一直没用过它,官方解释只有一句话:获取在匹配的元素集中的第一个元素的属性值。
大家都知道有的浏览器只要写disabled,checked就可以了,而有的要写成disabled = "disabled",checked="checked",比如用attr("checked")获取checkbox的checked属性时选中的时候可以取到值,值为"checked"但没选中获取值就是undefined。
jq提供新的方法“prop”来获取这些属性,就是来解决这个问题的,以前我们使用attr获取checked属性时返回"checked"和"",现在使用prop方法获取属性则统一返回true和false。
那么,什么时候使用attr(),什么时候使用prop()?
1.添加属性名称该属性就会生效应该使用prop();
2.是有true,false两个属性使用prop();
3.其他则使用attr();
项目中jquery升级的时候大家要注意这点!
以下是官方建议attr(),prop()的使用:
Attribute/Property | .attr() |
.prop() |
---|---|---|
accesskey | √ | |
align | √ | |
async | √ | √ |
autofocus | √ | √ |
checked | √ | √ |
class | √ | |
contenteditable | √ | |
draggable | √ | |
href | √ | |
id | √ | |
label | √ | |
location ( i.e. window.location ) | √ | √ |
multiple | √ | √ |
readOnly | √ | √ |
rel | √ | |
selected | √ | √ |
src | √ | |
tabindex | √ | |
title | √ | |
type | √ | |
width ( if needed over .width()) |
√ |
您可能感兴趣的文章:
- Jquery中attr与prop的区别详解
- jQuery获取attr()与prop()属性值的方法及区别介绍
- jQuery中attr()与prop()函数用法实例详解(附用法区别)
- jquery判断复选框选中状态以及区分attr和prop
- jQuery中 prop() attr()使用详解
- jquery中attr和prop的区别分析
- jQuery中attr()和prop()在修改checked属性时的区别
- jQuery学习之prop和attr的区别示例介绍
- jquery获取自定义属性(attr和prop)实例介绍
- jquery 获取自定义属性(attr和prop)的实现代码
- 关于jquery中attr()和prop()方法的区别
相关文章推荐
- jquery中prop()方法和attr()方法的区别浅析
- jquery中prop()方法和attr()方法的区别浅析
- jquery中prop()方法和attr()方法的区别浅析
- jquery中prop()方法和attr()方法的区别浅析
- jquery中prop()方法和attr()方法的区别浅析
- 【16】jquery中prop()方法和attr()方法的区别浅析
- jquery中prop()方法和attr()方法的区别浅析
- jquery中prop()方法和attr()方法的区别浅析
- jquery中prop()方法和attr()方法的区别浅析
- jQuery中prop()方法和attr()方法的区别浅析
- jquery中prop()方法和attr()方法的区别浅析
- jquery中prop()方法和attr()方法的区别浅析
- jquery中prop()方法和attr()方法的区别浅析
- jquery中prop()方法和attr()方法的区别浅析
- jquery中prop()方法和attr()方法的区别浅析
- jquery中prop()方法和attr()方法的区别浅析
- jquery中prop()方法和attr()方法的区别浅析
- jquery中prop()方法和attr()方法的区别浅析
- jquery中prop()方法和attr()方法的区别浅析
- jquery中prop()方法和attr()方法的区别浅析