问题
this question isn't related to jQuery
itself but I found a plugin named Metadata
found there and one of the example uses custom tag attribute: <li data="{some:'random', json: 'data'}">...</li>
.
Q: Is that cross-browser? Will this fail when validating markup?
Thanks.
回答1:
The browser won't care, since very, very few browsers actually validate the HTML. It will fail if you try to treat it as XHTML though, since it isn't valid XHTML.
回答2:
The browser wont care. Most (if not all browsers) just ignore illegal attributes. If you try to validate it, it WILL fail however. What you need to do is figure out if you're OK with this. If you are just keep the doctype. If not change the doctype. One thing to note is that even if you keep the doctype and the illegal attribute it wont impact your site in any way that it doesnt validate.
In fact your markup might still validate if the data attribute is being added after the page loads - which means that at the point the validation occurs the data attribute wont be there.
来源:https://stackoverflow.com/questions/3974139/what-happens-if-you-use-custom-attribute-in-a-html-tag