Custom Attributes of HTML tags

Source: Internet
Author: User

HTML tags can customize attributes, but we need to consider compatibility issues in IE, Firefox, and chrome.
For example:

  1. <Div id = "newtest" myattr = "getattr"> </div>

Copy code

Here "myattr" is the custom attribute of this tag.

If an attribute is defined but cannot be used, this attribute has no meaning. The next step is how to call the value of our custom attribute.

In IE browser, we can directly call it after obtaining the object.

  1. Document. getelementbyid ("newtest"). myattr;

Copy code

In IE, we can also directly assign values to them and dynamically generate a custom attribute:

  1. Document. getelementbyid ("newtest"). newattr = "new ";

Copy code

In Firefox and Google browsers, we can call the getattribute method:

  1. Document. getelementbyid ("newtest"). getattribute ("myattr ");

Copy code

In Firefox and Google browsers, we can use the setattribute method to generate and set a custom attribute:

  1. Document. getelementbyid ("newtest"). setattribute ("newattr", "new ");

Copy code

Custom Attributes are generally used to store data or relevant bases. Custom Attributes are useful based on actual conditions.

Test Browser: IE8, Firefox 8.01, chrome 17.0.963.46 m
Test results: custom attributes can be obtained.

In addition, if you do not want to make compatibility judgments, you can use the ATTR method of jquery to obtain and set custom attribute values. Currently, the test results are fully compatible.

  1. $ ("# Newtest"). ATTR ("myattr ");
  2. $ ("# Newtest"). ATTR ("newattr", "new ");
From http://www.cnblogs.com/ayforver/archive/2012/02/15/2352161.html

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.