Home >Web Front-end >JS Tutorial >Access to DOM element attributes that require special processing_DOM

Access to DOM element attributes that require special processing_DOM

WBOY
WBOYOriginal
2016-05-16 18:16:531149browse
Copy code The code is as follows:

var props = {
 'for' : 'htmlFor',
'class': 'className',
readonly: 'readOnly',
maxlength: 'maxLength',
cellspacing: 'cellSpacing',
rowspan: 'rowSpan',
colspan: 'colSpan',
tabindex: 'tabIndex',
usemap: 'useMap',
frameborder: 'frameBorder'
}

To digress, use If you create an object in json format, it is recommended that attributes do not require single quotes or double quotes, except for some ecmascript keywords, such as 'for', 'class', etc. above. It is recommended to use
single quotes instead of single quotes. Quotation marks are definitely better than double quotation marks and more compliant with specifications, but a habit is still very important. Do not have single quotation marks and double quotation marks appearing in the code.
A simple example:
Copy code The code is as follows:





If you want to get a certain attribute value of a DOM element directly through the access method elem.propertyName, you must make a simple change to the above attribute access,

For example, in the above example, you want to get the for attribute value of label.
Copy code The code is as follows:

var ele = document.getElementById('test');
var val = ele.htmlFor;
//or
val = ele['htmlFor'];
//If accessed through standard DOM methods, no special processing is required
val = ele.getAttribute('for');
val = ele.getAttributeNode('for').value;

Similarly for class, readonly, etc. access, the above types Access methods are equally suitable.

Personally, I think that accessing directly through attributes may be faster than accessing through DOM methods.
The following article will introduce access to special attributes.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn