HTML tags can customize attributes, but we need to consider compatibility issues in IE, Firefox, and chrome.
For example:
- <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.
- Document. getelementbyid ("newtest"). myattr;
Copy code
In IE, we can also directly assign values to them and dynamically generate a custom attribute:
- Document. getelementbyid ("newtest"). newattr = "new ";
Copy code
In Firefox and Google browsers, we can call the getattribute method:
- 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:
- 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.
- $ ("# Newtest"). ATTR ("myattr ");
- $ ("# Newtest"). ATTR ("newattr", "new ");
From http://www.cnblogs.com/ayforver/archive/2012/02/15/2352161.html