jQuery函数attr()和prop()的区别

来源:互联网 发布:4g网络怎么设置 编辑:程序博客网 时间:2024/05/19 15:42

“attr”和”prop”分别是单词”attribute”和”property”的缩写,并且它们均表示”属性”的意思。但attribute表示HTML文档节点的属性,property表示JS对象的属性。

<!-- 这里的id、class、data_id均是该元素文档节点的attribute --><div id="message" class="test" data_id="123"></div><script type="text/javascript">// 这里的name、age、url均是obj的propertyvar obj = { name: "CodePlayer", age: 18, url: "http://www.365mini.com/" };</script>

在jQuery中,prop()函数的设计目标是用于设置或获取指定DOM元素(指的是JS对象,Element类型)上的属性(property);attr()函数的设计目标是用于设置或获取指定DOM元素所对应的文档节点上的属性(attribute)。

<!-- attr()函数针对的是该文档节点的attribute --><div id="message" class="test" data_id="123"></div><script type="text/javascript">// prop()函数针对的是该DOM元素(msg)自身的propertyvar msg = document.getElementById("message");var $msg = $(msg);</script>

当然,在jQuery的底层实现中,函数attr()和prop()的功能都是通过JS原生的Element对象(如上述代码中的msg)实现的。attr()函数主要依赖的是Element对象的getAttribute()和setAttribute()两个方法。prop()函数主要依赖的则是JS中原生的对象属性获取和设置方式。

<div id="message" class="test" data_id="123"></div><script type="text/javascript">var msg = document.getElementById("message");var $msg = $(msg);/* *** attr()依赖的是Element对象的element.getAttribute( attribute ) 和 element.setAttribute( attribute, value ) *** */// 相当于 msg.setAttribute("data_id", 145);$msg.attr("data_id", 145);// 相当于 msg.getAttribute("data_id");var dataId = $msg.attr("data_id"); // 145/* *** prop()依赖的是JS原生的 element[property] 和 element[property] = value; *** */// 相当于 msg["pid"] = "pid值";$msg.prop("pid", "pid值");// 相当于 msg["pid"];var testProp = $msg.prop("pid"); // pid值</script>

当然,jQuery对这些操作方式进行了封装,使我们操作起来更加方便(比如以对象形式同时设置多个属性),并且实现了跨浏览器兼容。

此外,虽然prop()针对的是DOM元素的property,而不是元素节点的attribute。不过DOM元素某些属性的更改也会影响到元素节点上对应的属性。例如,property的id对应attribute的”id”,property的className对应attribute的”class”。

<div id="message" class="test" data_id="123"></div><script type="text/javascript">var msg = document.getElementById("message");var $msg = $(msg);document.writeln( $msg.attr("class") ); // test$msg.prop("className", "newTest");// 修改className(property)导致class(attitude)也随之更改document.writeln( $msg.attr("class") ); // newTest</script>

注意:

  • 由于attr()函数操作的是文档节点的属性,因此设置的属性值只能是字符串类型,如果不是字符串类型,也会调用其toString()方法,将其转为字符串类型。

  • 在jQuery1.6及以后版本中,attribute的”checked”、”selected”、”disabled”就是表示该属性初始状态的值;property的checked、selected、disabled才表示该属性实时状态的值(值为true或false)。

  • 在jQuery 1.6及以后版本中,请使用prop()函数来设置或获取checked、selected、disabled等属性。对于其它能够用prop()实现的操作,也尽量使用prop()函数。

示例代码如下:

<input id="uid" type="checkbox" checked="checked" value="1"><script type="text/javascript">// 当前jQuery版本为1.11.1var uid = document.getElementById("uid");var $uid = $(uid);document.writeln( $uid.attr("checked") ); // checkeddocument.writeln( $uid.prop("checked") ); // true// 取消复选框uid的选中(将其设为false即可)// 相当于 uid.checked = false;$uid.prop("checked", false);// attr()获取的是初始状态的值,即使取消了选中,也不会改变document.writeln( $uid.attr("checked") ); // checked// prop()获取的值已经发生变化document.writeln( $uid.prop("checked") ); // false</script>

转自:http://www.365mini.com/page/jquery-attr-vs-prop.htm

0 0
原创粉丝点击