This article mainly introduces the differences between property and attribute in JavaScript. This article summarizes the differences in definitions, similarities, differences, and browser compatibility, we recommend that you select property first. For more information, see
1. definition
Property: Attribute. all HTML elements are represented by the HTMLElement type. the HTMLElement type directly inherits from the Element and adds some attributes, each added attribute corresponds to each HTML element with the following five standard features: id, title, lang, dir, className. A DOM node is an object. Therefore, it can add custom attributes and methods like other JavaScript objects. The property value can be of any data type and is case sensitive. Custom property does not appear in html code and only exists in js.
Attribute: different from property, attribute can only be a string and is case insensitive. it appears in innerHTML and can list all attributes through the class array attributes.
2. similarities
Standard DOM properties are synchronized with attributes. Accepted (non-custom) features are added to DOM objects as attributes. For example, id, align, style, etc. in this case, you can operate the property or the DOM method using the operation features, such as getAttribute. However, the feature name passed to getAttribute () is the same as the actual feature name. Therefore, when obtaining the feature value of the class, you must pass in "class ".
3. Differences
1). For some standard feature operations, the values obtained by getAttribute and period (.) are different. Event handlers such as href, src, value, style, and onclick.
2). href: getAttribute obtains the actual value of href, while the DOT number obtains the complete url, which is different from the browser.
The code is as follows:
Script
Var a = document. body. children [0]
A. href = '/'
Alert ('attribute: '+ a. getAttribute ('href '))//'/'
Alert ('property: '+ a. href) // IE:'/', others: full URL
Script
Retrieving src values is similar to href, But IE will return full URL;
The value also has some built-in properties for one-way synchronization.
For example, the input. value is synchronized from the attribute (that is, the property is synchronized from the attribute)
The code is as follows:
Script
Var input = document. body. children [0];
Input. setAttribute ('value', 'new ');
Alert (input. value); // 'new', input. value changed
Alert (input. getatrriert (value); // 'new'
Script
However, attribute cannot be synchronized from property:
The code is as follows:
Script
Var input = document. body. children [0];
Input. value = 'new ';
Alert (input. getAttribute ('value'); // 'markup', not changed!
Script
GetAttribute obtains the initial value, while the vertex number obtains the initial value or. value after modification. for example, when a visitor inputs certain characters, the 'value' attribute maintains the original value after the property is updated. The original value can be used to check whether the input changes or reset it.
For event handlers such as style and onclick, the getAttribute method returns a string during access, and the vertex number returns the corresponding object and event processing function.
For the checked attribute in input
The code is as follows:
Script
Var input = document. body. children [0]
Alert (input. checked) // true
Alert (input. getAttribute ('checked') // empty string
Script
GetAttribute obtains the value you actually set. The vertex number returns a boolean value.
Differences in browser compatibility
1. in IE <9 browser, you can use the dot and getAttribute to access custom attributes between each other.
2. IE <8 (including IE8 IE7 compatibility modes). The property and attribute are the same. Attribute is case-insensitive. in this case, when you use getAttribute to access the feature, the browser selects the value that appears for the first time.
The code is as follows:
Document. body. abba = 1 // assign property (now can read it by getAttribute)
Document. body. ABBA = 5 // assign property with another case
// Must get a property named 'ABBA' in case-insensitive way.
Alert (document. body. getAttribute ('ABBA') // 1
Select property first
In actual application, 98% of DOM operations use properties.
There are only two scenarios where attributes needs to be used
1. Custom HTML attributes because it is not synchronized to DOM property.
2. access the built-in HTML attributes. these attributes cannot be synchronized from the property. For example, the value of the INPUT tag.