jQuery的prop和attr方法之间区别

JQuery.attr():
Get the value of an attribute for the first element in the set of matched elements.

JQuery. Prop():
Gets the value of a property for the first element in the set of matched elements.

Reference MSDN:
for a checkbox (jquery 1.6+)
<input id="check1" checked="checked" type="checkbox" />

.attr(‘checked‘)     //returns checked
.prop(‘checked‘)     //returns true
.is(‘:checked‘)     //returns true

Prop() method returns Boolean value for checked, selected, disabled, readOnly..and so on while attr returns defined string. So, you can directly use .prop("checked") in if condition. SelectedIndex, tagName, nodeName, nodeType, ownerDocument, defaultChecked, and defaultSelected..and so on should be retrieved and set with the .prop() method. These do not have corresponding attributes and are only properties. .attr() calls .prop() internally so .attr() method will be slightly slower than accessing them directly through .prop().

一些内容(摘录来自网络)。

时间: 2024-08-25 14:06:15

jQuery的prop和attr方法之间区别的相关文章

jquery中prop()与attr()方法的区别

一.prop() 简单来说是当需要判断真假时使用,如复选框时: if( $(this).prop('checked')){ //当返回true时在这里调用 }else{ //当返回false时在这里调用 } TIPS:相关判断$(this).is(':checked')--这个用得酷点 二.attr() 简单来说当你要获得某个属性及其值时使用,如获得p标签的id时: $('p').attr('id'); 或将其设置id为pId时: $('p').attr('id','pId');

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

jquery1.6中新加了一个方法prop(),一直没用过它,官方解释只有一句话:获取在匹配的元素集中的第一个元素的属性值. 大家都知道有的浏览器只要写disabled,checked就可以了,而有的要写成disabled = "disabled",checked="checked",比如用attr("checked")获取checkbox的checked属性时选中的时候可以取到值,值为"checked"但没选中获取值就是un

prop()方法和attr()方法的区别

prop()方法和attr()方法的区别:关于这两个方法的具体用法这里就不多介绍了,可以参阅prop()和attr()方法相关手册即可.这两个方法作用好像是一模一样,其实绝大多数作用都是一样,只有在一些特殊的情况下才有可能出现一些差异,下面就结合实例实例介绍一下在这两个方法的区别.先看一段代码实例: <!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name=&q

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中html 与 text方法的区别

jquery中html 与 text方法的区别 24 May 2012/in 网站设计和开发 /by Bruce 接鉵jquery的时间并不长,以前都是用直接用js写的,现在发现在jquery这个框架用起来很方便,不但代码量少了,使用也比较简单,对于浏览器的兼容问题也不用担心,在使用的过程中也会遇到一些疑问,在html标签中附加子标签时所用的方法html()与text()的区别. 通常在用jquery写ajax时,都会用到html()这个方法,而不用text()这个方法,他们之间有什么区别呢?

jQuery中的bind() live() delegate()之间区别分析

jQuery中的bind() live() delegate()之间区别分析 首先,你得要了解我们的事件冒泡(事件传播)的概念,我先看一张图 1.bind方式 $('a').bind('click',function (){ alert('click'); }) 解析:这种方式最简单,jq扫描文档找出所有的a,让将函数绑定到每个元素的click事件上 2.live方式 $('a').live('click',function (){ alert('click'); }) 解析:jq将函数绑定到$

prop()方法和attr()方法以及区别

prop()方法:定义和用法prop() 方法设置或返回被选元素的属性和值.当该方法用于返回属性值时,则返回第一个匹配元素的值.当该方法用于设置属性值时,则为匹配元素集合设置一个或多个属性/值对.注意:prop() 方法应该用于检索属性值,例如 DOM 属性(如 selectedIndex, tagName, nodeName, nodeType, ownerDocument, defaultChecked, 和 defaultSelected).提示:如需检索 HTML 属性,请使用 attr

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

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

jquery之prop和attr区别

最近用jquery比较多,处理属性的时候发现之前并没有注意到的问题. 平时处理属性都用attr,比如获取checkbox的checked值 或者设置checkbox的checked的值 <input type="checkbox" id="allCheck"> $('#allCheck').attr('checked') $('#allCheck').attr('checked','checked') //发现选项并未被选中,只是在有些情况下发生 转自: