Custom Attribute on HTML Element [duplicate]
Possible Duplicates:
Storing arbitrary info in HTML tags for JavaScript? What are the concrete risks of using custom HTML attributes? Custom attributes - Yay or nay?
What are the drawbacks of adding a custom attribute on a standard HTML element?
For example:
<intput type="textbox" id="MyId" MyCustomeAttribute="MyData" />By adding the MyCustomAttribute, I've made the document invalid, but what are the actual implecations? What problems, if any, will this cause?
I'd be using Javascript to read the values of the custom attributes.
Thanks
The drawback is that you can't use validation tools, because they'll complain about an "invalid" attribute.
HTML5 provides a valid way for us to do this, though (huzzah!): Use the prefix data-
for your own attributes, e.g., data-mycustomattribute
. And it works today in all major browsers.
It may cause compatibility problems sooner or later.
Rather prefix the attribute name with data-
, e.g. data-MyCustomeAttribute
. There it is for.
Usually most browser ignore attiributes they can't understand. However there should be another way to do what you are trying to do with a valid markup
I believe it's part of the spec for HTML5 and should therefor validate using a <html>
doctype. But as we all know HTML5 is not fully implemented yet across browsers so you your going to have to wait a while for it to validate
You can combine validatability and customizability if you start from an xml source, with your own dtd, and transform it to (valid) html with your javascript.
精彩评论