Home  >  Article  >  Web Front-end  >  Attribute and property in HTML_html/css_WEB-ITnose

Attribute and property in HTML_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:40:371218browse

1. Overview

Attribute and property are two concepts that are often confused.

To put it simply, property is accessed in JS code:

document.getElementByTagName('my-element').prop1 = 'hello';

attribute is similar This kind:

The way to access attribute in JS code is getAttribute and setAttribute:

document.getElementByTagName( 'my-element').setAttribute('attr1','Hello');

document.getElementByTagName('my-element').getAttribute('attr1','Hello');

2. Differences

In most cases, the two are equivalent. In web standards, it is often stipulated that an attribute "reflects" a property with the same name. But there are still many exceptions.

1. Inconsistent names

The most typical one is className. In order to avoid JavaScript reserved words, the property corresponding to the class attribute in JS is className.