对于自定义标签在HTML5中是否有效,我一直无法找到一个明确的答案,比如:

<greeting>Hello!</greeting>

我在说明书中什么都没发现:

http://dev.w3.org/html5/spec/single-page.html

而且自定义标记似乎无法使用W3C验证器进行验证。


当前回答

基本自定义元素和属性

自定义元素和属性在HTML中是有效的,前提是:

元素名称是小写的,以x-开头 属性名小写,以data-开头

例如,<x-foo data-bar="gaz"/>或<br data-bar="gaz"/>。

元素的通用约定是x-foo;推荐使用x厂商特性。

这可以处理大多数情况,因为开发人员很少需要注册他们的元素所带来的所有功能。语法也足够有效和稳定。下面是更详细的解释。

高级自定义元素和属性

到2014年,有了一种新的、经过很大改进的方法来注册自定义元素和属性。它不能在ie9或Chrome/Firefox 20等较老的浏览器中运行。但是它允许你使用标准的HTMLElement接口,防止冲突,使用非x-*和非data-*名称,并定义自定义的行为和语法。它需要一些奇特的JavaScript,详见下面的链接。

HTML5岩石-在HTML中定义新元素 WebComponents.org -自定义元素介绍 Web组件:自定义元素

关于基本语法的有效性

使用data-*自定义属性名已经完全有效了一段时间,甚至适用于旧版本的HTML。

W3C - HTML5:可扩展性

至于自定义的(未注册的)元素名称,W3C强烈建议不要使用它们,并认为它们不符合规范。但是浏览器需要支持它们,x-*标识符不会与未来的HTML规范冲突,x-vendor-feature标识符也不会与其他开发人员冲突。自定义DTD可用于绕过任何挑剔的浏览器。

以下是官方文件中的一些相关摘录:

"Applicable specifications MAY define new document content (e.g. a foobar element) [...]. If the syntax and semantics of a given conforming HTML5 document is unchanged by the use of applicable specification(s), then that document remains a conforming HTML5 document." "User agents must treat elements and attributes that they do not understand as semantically neutral; leaving them in the DOM (for DOM processors), and styling them according to CSS (for CSS processors), but not inferring any meaning from them." "User agents are not free to handle non-conformant documents as they please; the processing model described in this specification applies to implementations regardless of the conformity of the input documents." "The HTMLUnknownElement interface must be used for HTML elements that are not defined by this specification."

W3C - HTML5:一致性文档 HTML标准:DOM元素

其他回答

我想指出的是,“有效”这个词在这里有两种不同的含义,其中任何一种都是潜在的,嗯,有效的。

Should an HTML document with custom tags be considered valid HTML5? The answer to this is clearly "no." The spec lists exactly what tags are valid in what contexts. This is why an HTML validator will not accept a document with custom tags, or with standard tags in the wrong places (like an "img" tag in the header). Will an HTML document with custom tags be parsed and rendered in a standard, clearly-defined way across browsers? Here, perhaps surprisingly, the answer is "yes." Even though the document would not technically be considered valid HTML5, the HTML5 spec does specify exactly what browsers are supposed to do when they see a custom tag: in short, the custom tag acts kind of like a <span> - it means nothing and does nothing by default, but it can be styled by HTML and accessed by javascript.

自定义标记在HTML5中无效。但目前浏览器支持解析它们,你也可以使用css来使用它们。因此,如果你想为当前的浏览器使用自定义标记,那么你可以。但是,一旦浏览器实现了严格用于解析HTML内容的W3C标准,这种支持可能就会消失。

data-*属性在HTML5中有效,甚至在HTML4中,所有web浏览器都尊重它们。 添加新标签在技术上是可以的,但不建议这样做,因为:

它可能会与将来添加的内容相冲突,而且 使HTML文档无效,除非通过JavaScript动态添加。

我只在谷歌不关心的地方使用自定义标签,在一个游戏引擎iframe中,我做了一个<日志>标签,包含<msg>, <错误>和<警告> -但只通过JavaScript。根据验证器,它是完全有效的。它甚至可以在Internet explorer中使用它的样式!;]

自定义HTML元素是一个新兴的W3标准,我一直在为它做出贡献,它允许你用解析器声明和注册自定义元素,你可以在这里阅读规范:W3 Web Components自定义元素规范。此外,微软支持一个名为X-Tag的库(由前Mozilla开发人员编写)——它使使用Web组件更加容易。

这是可能的,也是允许的:

用户代理必须处理它们不处理的元素和属性 理解为语义中立的;将它们留在DOM中(用于DOM 处理器),并根据CSS(对于CSS处理器)对它们进行样式化, 但并没有从中推断出任何意义。

http://www.w3.org/TR/html5/infrastructure.html#extensibility-0

但是,如果你想要增加交互性,你需要让你的文档无效(但仍然是完整的功能),以适应IE的7和8。

见http://blog.svidgen.com/2012/10/building-custom-xhtml5-tags.html(我的博客)