Introduction to the custom attributes of JavaScript to read DOM objects (TAGS)

Source: Internet
Author: User
Talking about how JavaScript reads Custom Attributes of DOM objects (TAGS). DOM objects are a basic element for Javascript. Generally, JavaScript will always operate on them. We can easily add custom attributes to it, such:

var test = document.getElementById("test"); test.adang = "adang"; alert(test.adang);

We will find that an attribute named adang has been added to the DOM element whose id is test, and this attribute can be called in js. I often use this method when writing JavaScript, so that I can easily add some special data to a dom object. The DOM object is like a very useful container, you can put a pile of data in it.

Another question arises: can these attributes be added to js? Can they be added to tags like flex? In fact, attributes such as id and src can be added to js or tags. js can both obtain data. Here we need to say that the class is special. The label uses the class, but the class is used for calling in js.

Attributes supported by html such as id, title, and src can be set in tags and accessed by js. So what if it is a custom attribute like adang in my above example? Can DOM be accessed? I did an experiment as follows:

New document
 
 
 
 
Related Article

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.