jQuery中attr和prop在修改checked属性时的区别

 我来答
匿名用户
推荐于2017-12-16
展开全部

.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中正常运行。

 

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


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


育知同创教育
2018-07-28 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
展开全部
对于checkbox,如果没有定义checked="checked",alert($.attr("checked")) 的结果是undefined。
用prop方法,则输出的是true或者false, $("input[type='checkbox']").prop("checked", true);
修改checked 使用prop更适合。
修改固有属性时,最好使用prop;
修改自定义属性时,使用attr。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
青鸟中关村专家
2018-08-04 · 知道合伙人软件行家
青鸟中关村专家
知道合伙人软件行家
采纳数:1734 获赞数:8439
就职于北大青鸟中关村,自2004年踏入北大青鸟这个行业,已经有11年工作经验和8年的培训经验,寓教于乐

向TA提问 私信TA
展开全部
prop在修改checked属性的时候使用true和false即可
attr在修改checked属性的时候要使用checked
checked属性即分为attribute->checked,和property->true,false
对于一个checkbox,若未定义checked="checked",alert($.attr("checked")) 的结果是undefined。若已定义则结果是checked。attribute并不随着checkbox的状态变化而改变。
  使用prop($.attr("checked"))的话输出则分别为false和true。property则随其变化而变化。
  所以在修改checked属性时要使用prop()。prop()在jQuery1.6版本后新增。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
就烦条0o
2018-08-02 · 知道合伙人软件行家
就烦条0o
知道合伙人软件行家
采纳数:33315 获赞数:46484
从事多年系统运维,喜欢编写各种小程序和脚本。

向TA提问 私信TA
展开全部
.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中正常运行。

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

注意:一些DOM元素的properties也被列在下面,但是仅运行在新的.prop()方法中
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式