这两者太容易混淆了,简单来说,DOM对象拥有property属性,是一种特定的变量,property可以指定为任意的变量,如字符串,数字,对象等,jquery1.6以后的版本可以使用$.prop(key)方法获得properties,举个例子
<a href='page2.html' class='link classes' name='linkName' id='linkID'>Hi</a>
$('#linkID').prop('href'); // returns "http://example.com/page2.html"
$('#linkID').prop('name'); // returns "linkName"
$('#linkID').prop('id'); // returns "linkID"
$('#linkID').prop('className'); // returns "link classes"
当然prop方法也可以更新数据:
set
$('#linkID').prop('href', 'page1.html');
get
$('#linkID').prop('href'); // returns "http://example.com/page1.html"
html拥有attribute,attribute只有字符串值没有别的。所以建议使用property而不是使用attribute。
<input type="checkbox" checked=true/>
$('input').prop('checked'); // returns true
$('input').attr('checked'); // returns "checked"
当html元素有默认值时,即使是值发生了改变,attribute也只是保持默认的。
<input type="text" name="username" value="user123">
$('input').prop('value', '456user');
$('input').prop('value'); // returns "456user"
$('input').attr('value'); // returns "user123"
但是在用户自定义数据时attribute才发挥出它的作用,
<input type="text">
$('input').attr('customAttribute', 'something custom');
$('input').attr('customAttribute'); // returns "something custom"
$('input').prop('customAttribute'); // returns undefined
property也可以实现自定义属性,但是不推荐使用,不是一个好的实践。
<input type="text">
$('input').prop('customAttribute', 'something custom');
$('input').prop('customAttribute'); // returns "something custom"
$('input').attr('customAttribute'); // returns undefined
reference:http://stackoverflow.com/questions/5874652/prop-vs-attr/5884994#5884994
http://api.jquery.com/prop/