如果使用无效/非标准的HTML属性(用于数据)会怎样? [英] What happens if an invalid/nonstandard HTML attribute is used (for data)?

查看:38
本文介绍了如果使用无效/非标准的HTML属性(用于数据)会怎样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题.

但是,我想知道在html 5上使用无效属性时会发生什么?

However, I want to know what happen when I use invalid attribute on html 5?

例如,如果我想在标签上添加attr'City'.

For example, if I want to add a attr 'City' on a tag.

<div data-city="etc"></div> 

是有效的方法.

但是,通过这种方式可以很好地工作

However, there is perfectly working fine with this way

<div city="etc"></div> 

我想知道我不应该使用'city'而不是'data-city' ??

I want to know what is the result that I should not use 'city' instead of 'data-city'??

如果仅用于传递javascrpit的数据,我一般只能使用'city'吗?

If only for use for passing data for javascrpit, can I use only 'city' in general??

非常感谢您的建议.

推荐答案

不幸的是,这是不正确的.但是,所有(现代)浏览器都可以做到这一点(只要能够将其显示并保存在DOM中),因为它们努力与所有(甚至是不良的)HTML代码兼容,但这并不是一个好选择.真实属性.您的代码将无法验证为真正的W3规范HTML 5代码,但仍然可以使用.

Unfortunately, this is not correct. However, all (modern) browsers will be okay with this (as far as being able to display it and keep it in the DOM) as they strive to be compatible with all (even the bad) HTML code, but it will not be a true attribute. Your code will not validate as true W3 specification HTML 5 code, but it should still work.

如果需要使用"city",则可能需要使用XHTML,您可以在其中为自己的元素指定自己的属性.

If you need to use 'city', you may want to use XHTML, where you can specify your own attributes to your own elements.

这篇关于如果使用无效/非标准的HTML属性(用于数据)会怎样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆