jQuery学习之prop和attr的区别

http://blog.sina.com.cn/s/blog_655388ed01017cnc.html

.prop()

1、.prop( propertyName )

获取匹配集合中第一个元素的Property的值

2、

.prop( propertyName, value )

.prop( map )

.prop( propertyName, function(index, oldPropertyValue) )

给匹配元素集合设定一个或多个属性

.prop()和 .attr()区别

下面是关于jQuery1.6和1.6.1中Attributes模块变化的描述,以及.attr()方法和.prop()方法的首选使用

Attributes模块的变化是移除了attributes和properties之间模棱两可的东西,但是在jQuery社区中引起了一些混乱,因为在1.6之前的所有版本中都使用一个方法(.attr())来处理attributes和properties。但是老的.attr()方法有一些bug,很难维护。jQuery1.6.1对Attributes模块进行了更新,并且修复了几个bug。

elem.checked true (Boolean) Will change with checkbox state

$(elem).prop("checked") true (Boolean) Will change with checkbox
state

elem.getAttribute("checked") "checked" (String) Initial state of the
checkbox; does not change

$(elem).attr("checked")(1.6) "checked" (String) Initial state of the
checkbox; does not change

$(elem).attr("checked")(1.6.1+) "checked" (String) Will change with
checkbox state

$(elem).attr("checked")(pre-1.6) true (Boolean) Changed with checkbox
state

if ( elem.checked )

if ( $(elem).prop("checked") )

if ( $(elem).is(":checked") )

这三个都是返回Boolean值。

为了让jQuery1.6中的.attr()方法的变化被理解的清楚些,

下面是一些使用.attr()的例子,虽然在jQuery之前的版本中能正常工作,但是现在必须使用.prop()方法代替:

首先,window或document中使用.attr()方法在jQuery1.6中不能正常运行,因为window和document中不能有attributes。它们包含properties(比如:location或readyState),必须使用.prop()方法操作或简单地使用javascript原生的方法。在jQuery1.6.1中,window和document中使用.attr()将被自动转成使用.prop,而不是抛出一个错误。

其次,checked,selected和前面提到的其它boolean
attributes,因为这些attributes和其相应的properties之间的特殊关系而被特殊对待。基本上,一个attribute就是以下html中你看到的:

<input type=”checkbox” checked=”checked”>

boolean attributes,比如:checked,仅被设置成默认值或初始值。在一个checkbox的元素中,checked
attributes在页面加载的时候就被设置,而不管checkbox元素是否被选中。

properties就是浏览器用来记录当前值的东西。正常情况下,properties反映它们相应的attributes(如果存在的话)。但这并不是boolean
attriubutes的情况。当用户点击一个checkbox元素或选中一个select元素的一个option时,boolean
properties保持最新。但相应的boolean attributes是不一样的,正如上面所述,它们仅被浏览器用来保存初始值。

$(“:checkbox”).get(0).checked = true;

// Is the same as $(":checkbox:first").prop(“checked”, true);

在jQuery1.6中,如果使用下面的方法设置checked:

$(“:checkbox”).attr(“checked”, true);

将不会检查checkbox元素,因为它是需要被设置的property,但是你所有的设置都是初始值。

然而,曾经jQuery1.6被释放出来的时候,jQuery团队明白当浏览器仅关心页面加载时,设置一些值不是特别的有用。所以,为了保持向后兼容性和.attr()方法的有用性,我们可以继续在jQuery1.6.1中使用.attr()方法取得和设置这些boolean
attributes。

最普通的attributes是checked,selected,disabled和readOnly,但下面是jQuery1.6.1支持的使用.attr()动态地取得和设置boolean
attributes/properties的完整列表:

autofocus, autoplay, async, checked, controls, defer, disabled,

hidden, loop, multiple, open, readonly, required, scoped, selected

还是建议使用.prop()方法来设置这些boolean
attributes/properties,
即使这些用例没有转换成使用.prop()方法,但是你的代码仍然可以在jQuery1.6.1中正常运行。

1.8.3
还可以。1.9.0及以上都不行了。

 

下面是一些attributes和properties的列表,正常情况下,应该使用其对应的方法(见下面的列表)来取得和设置它们。下面的是首用法,但是.attr()方法可以运行在所有的attributes情况下。

注意:一些DOM元素的properties也被列在下面,但是仅运行在新的.prop()方法中

*例如: window.location

**如果需要在(if needed over) .width()

.attr()和.prop()都不应该被用来取值/设值。使用.val()方法代替(即使使用.attr("value","somevalue")
可以继续运行,就像1.6之前做的那样)

3、首选用法的概述

.prop()方法应该被用来处理boolean
attributes/properties以及在html(比如:window.location)中不存在的properties。其他所有的attributes(在html中你看到的那些)可以而且应该继续使用.attr()方法来进行操作。

上面的概述已经描述的够清楚了,我也没有必要再总结了。

参考文献:

http://hxq0506.iteye.com/blog/1046334

jQuery学习之prop和attr的区别,布布扣,bubuko.com

时间: 2024-08-02 11:01:50

jQuery学习之prop和attr的区别的相关文章

jquery中prop和attr的区别

jquery中prop和attr的区别 prop: prop(name|properties|key,value|fn) **概述** 获取在匹配的元素集中的第一个元素的属性值. 随着一些内置属性的DOM元素或window对象,如果试图将删除该属性,浏览器可能会产生错误. jQuery第一次分配undefined值的属性,而忽略了浏览器生成的任何错误 **参数** name String V1.6 属性名称 properties Map V1.6 作为属性的"名/值对"对象 key,v

关于jQuery表单选择中prop和attr的区别。

今天用jQuery学习表单这一章节的内容,再次遇到表单全选时,不能进行第二次全选的情况.反复查看测试仍然找不到是什么原因.后来在网上查到原来是jQuery1.6以后的版本用到的是prop.用attr的话不会多次实现,因为attr不会记录当前checkbox的选中状态. 表单这一章节内容让我感觉到有点吃力,总之好好努力吧! 以下是代码说明: <!DOCTYPE html> <html lang="en"> <head> <meta charset

【Jquery】prop与attr的区别

最近因项目需要用到复选框,其中一个控制全选. // 全选 $(".ckb_all").click(function(){ if($(this).attr("checked") == true){ $(":input[name='ckb_img']").attr("checked",true); }else{ $(":input[name='ckb_img']").attr("checked&quo

jQuery中prop() 和 attr()的区别

prop() 和 attr() 可能返回不同的值,先看下面一段代码 <script> $(document).ready(function(){ $("button").click(function(){ $("input").attr('checked') //attr('checked'): checked 或 undefined $("input").prop('checked')); //prop('checked'): tr

prop与attr的区别

与prop一样attr也可以用来获取与设置元素的属性. 区别在于,对于自定义属性和选中属性的处理. 选中属性指的是 checked,selected 这2种属性 1. 对于自定义属性 attr能够获取,prop不能获取 2. 对于选中属性 attr 只能获取初始值, 无论是否变化 prop 能够访问变化后的值,并且以true|false的布尔型返回. 所以在访问表单对象属性的时候,应该采用prop. <script src="http://how2j.cn/study/jquery.min

jquery中prop()和attr()的区别

在高版本的jquery引入prop方法后,什么时候该用prop?什么时候用attr?它们两个之间有什么区别?这些问题就出现了. 关于它们两个的区别,网上的答案很多.这里谈谈我的心得,我的心得很简单: 对于HTML元素本身就带有的固有属性,在处理时,使用prop方法. 对于HTML元素我们自己自定义的DOM属性,在处理时,使用attr方法. 上面的描述也许有点模糊,举几个例子就知道了. <a href="http://www.baidu.com" target="_sel

第82天:jQuery中prop()和attr()的区别

在高版本的jquery引入prop方法后,什么时候该用prop?什么时候用attr?它们两个之间有什么区别?这些问题就出现了. 关于它们两个的区别,网上的答案很多.这里谈谈我的心得,我的心得很简单: 对于HTML元素本身就带有的固有属性,在处理时,使用prop方法. 对于HTML元素我们自己自定义的DOM属性,在处理时,使用attr方法. 上面的描述也许有点模糊,举几个例子就知道了. <a href="http://www.baidu.com" target="_sel

jQuery prop和attr的区别

两者对比 jquery方法 原理 适合场景 缺陷 prop 解析原生property element.property radio/checkbox select标签 等需要读boolean和索引的场合 读不到自定义属性 如<a my='I'/> my属性读不到 attr 通过Attr API去读取 element.getAttribute(propertyName) 除prop场景外 可能读不到boolean或一些索引值 如checked,selectedIndex prop方法 例子 在控

高版本jquery的prop和attr使用区别的小例子

<script src="jquery.min.js"></script><script> function test(){ $("input").each(function(){ console.log($(this).prop("checked")); });} </script> <input type="radio" action="hello"