What happens if you use custom attribute in a HTML tag?

六月ゝ 毕业季﹏ 提交于 2019-12-23 09:59:58

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!