Access to DOM element attributes that require special processing

Source: Internet
Author: User

Copy codeThe 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'
}

When an object is created in json format, we recommend that you do not need single or double quotation marks for the attribute unless some keywords of ecmascript, such as the preceding 'for 'and 'class, we recommend that you use
Single quotation marks are not to say that single quotation marks must be better than double quotation marks and comply with specifications. However, it is very important to use single quotation marks instead of double quotation marks in the code.
A simple example:Copy codeThe Code is as follows: <label for = "username" id = "test"> name: </label> <BR> <input type = "text" id = "username" name = "username"/> <BR>

If you want to directly obtain an attribute value of a DOM element through access such as elem. propertyName, you must make a simple change to the preceding attribute access,

For example, in the above example, you want to get the label's for attribute value.Copy codeThe Code is as follows: var ele = document. getElementById ('test ');
Var val = ele.html;
// Or
Val = ele ['html'];
// You do not need to perform special operations if you use standard DOM methods.
Val = ele. getAttribute ('');
Val = ele. getAttributeNode ('for '). value;

The above methods are also suitable for class and readonly access.

In my opinion, direct access through attributes may be faster than DOM access.
The following article will introduce the access to special attributes.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.