A brief talk on JS read the custom attribute of the DOM Object (label) _javascript tips

Source: Internet
Author: User
Tags generator

Dom object for JS, is a very basic element, we write JS in general, will certainly operate on it. We can easily add custom attributes to it, such as:

<div id= "test" class= "Hello" ></div>

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

Test.adang = "Adang";

alert (Test.adang);

We'll find that we've added a property called Adang to this DOM element with test ID, and then in JS, you can call this property. I often use this method when writing JS, can easily add some special data to a DOM object, feel Dom object like a very good container, can put a pile of data in.

To further think of a problem, these properties can be added in JS, then whether it can be like Flex, in the tag to add it? In fact, like ID ah, src Ah such attributes, are can be added in JS, can also be added on the label, two ways JS can get data. Here to say a little, class is more special, the label used is class, in the JS call but to use classname to do.

Like IDs, TITLE,SRC properties that are supported in such HTML, can be set in the tag, and then JS access. So, what if it's a custom attribute like the Adang in my example above? Can DOM access it? An experiment was done, as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
 
 

With JS to extend the custom properties, the result is very normal output we want the results, IE and FF are normal.

Then I wrote the second piece of code, as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
 
 

This time, the extended attributes are written to the HTML tag. ie under the normal output ADANG,FF under the output is undefined.

But strangely, if you use the method provided by Dom GetAttribute (""), whether under IE or FF, you can get the custom attributes we write in the label.

So, to be compatible, we use GetAttribute ("") to get the value of the custom Label property.

The above article about JS read Dom objects (tags) of the custom attribute is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

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.