Home >Web Front-end >JS Tutorial >Introduction to the use of jQuery's attr and prop_jquery
attribute and property
Both attribute and property can be translated as attributes. In order to show the difference, these two words are usually translated as attributes and characteristics.
There are three nodes in the above HTML statement, namely Element "div", attribute "id", and Text "click here". Our most common attribute officially refers to the attribute type node. There is a special method for processing attributes in JavaScript. Function.getAttribute(name) / setAttribute(name,value). Of course, attributes are not just the ones we can see on HTML documents. We can customize attributes and add them to DOM nodes
Copy code
The attributes set through the setAttribute method will eventually be reflected in the attribute type node of the elementIt seems that attribute and property have nothing to do with each other, how come? . . Attribute and property are easily confused because many attribute nodes also have a corresponding property attribute. For example, the "id" attribute of the div above can also be obtained using t.id (in fact, most people obtain it this way). After changing the id through property, the id obtained with getAttibute is the updated id.
Copy code
Copy code
1. Regarding the build-in attribute, attribute and property share data. If the attribute is changed, it will affect the property, and vice versa. However, the custom attributes of the two are independent data. Even if the names are the same, they will not affect each other. , it looks like the picture below, but there is no distinction between IE6 and 7, and they still share custom attribute data
Copy code
3. For a property whose value is true/false, similar to the checked attribute of input, etc., the value obtained by attribute is the literal value of the HTML document, and the property obtains the calculation result. The change of property does not affect the literal value of attribute, but the change of attribute Will always calculate property
Copy code
Copy code
4. 对于一些和路径相关的属性,两者取得值也不尽相同,但是同样attribute取得是字面量,property取得是计算后的完整路径
attr和prop
相信看完上面内容,大家就明白为什么jQuery要添加prop方法了,在jQuery API中也有专门解释
Attributes VS. Properties
在一些特殊的情况下,attributes和properties的区别非常大。在jQuery1.6之前,.attr()方法在获取一些attributes的时候使用了property值,这样会导致一些不一致的行为。在jQuery1.6中,.prop()方法提供了一中明确的获取property值得方式,这样.attr()方法仅返回attributes。
比如,selectedIndex, tagName, nodeName, nodeType, ownerDocument, defaultChecked, 和defaultSelected应该使用.prop()方法获取/设置值。 在jQuery1.6之前这些不属于attribute的property需要用.attr()方法获取。这几个并没有相应的attibute,只有property。
关于布尔类型 attributes,比如一个这样的HTML标签,它在JavaScript中变量名为elem
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 |