HTML elements have very rich attributes. However, in some cases, it may be too tight. At this time, custom attributes play a very critical role.
Custom attributes of Html elements are very convenient to use, for example:
Suppose we need to limit it now. This button can only be clicked 2 times, and then it will become invalid.
The usual implementation method is to use global variables to record the number of clicks, but here we use custom attributes to implement this function and show the advantages of custom attributes; let’s modify the button above:
As you can see, I added a custom attribute clickCount to this button and set the initial value to 0; let’s write the js code to implement the function:
1. Add click event processing to button
2. Let’s write the function customAttributeDemo(obj)
For IE, using custom attributes is very simple, because IE automatically parses custom attributes into the DOM, which is no different from standard attributes. The version under IE:
Thanks to the netizen for the exchange, he gave the methods of getAttribute and setAttribute: