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

<greeting>Hello!</greeting>

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

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

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


当前回答

注意:下面的答案在2012年写的时候是正确的。从那以后,事情发生了一些变化。HTML规范现在定义了两种类型的自定义元素——“自主自定义元素”和“自定义内置元素”。前者可以去任何地方措辞内容是预期的;这是主体内的大部分位置,但不是例如ul或ol元素的子元素,或在除td、th或标题元素之外的表元素中。后者可以去任何它们扩展的元素可以去的地方。


这实际上是元素内容模型积累的结果。

例如,根元素必须是html元素。

html元素只能包含head元素和body元素。

The body element may only contain Flow content where flow content is defined as the elements: a, abbr, address, area (if it is a descendant of a map element), article, aside, audio, b, bdi, bdo, blockquote, br, button, canvas, cite, code, command, datalist, del, details, dfn, div dl, em, embed, fieldset, figure, footer, form, h1, h2, h3, h4, h5, h6, header, hgroup, hr, i, iframe, img, input, ins, kbd, keygen, label, map, mark, math, menu, meter, nav, noscript, object, ol, output, p, pre, progress, q, ruby, s, samp, script, section, select, small, span, strong, style (if the scoped attribute is present), sub, sup, svg, table, textarea, time, u, ul, var, video, wbr and Text

等等。

在任何情况下,内容模型都不会说“你可以把任何你喜欢的元素放在这个里面”,这对于自定义元素/标记来说是必要的。

其他回答

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

用户代理必须处理它们不处理的元素和属性 理解为语义中立的;将它们留在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(我的博客)

给出一个反映现代页面的更新答案。

自定义标记是有效的,

1)它们包含一个破折号

<my-element>

2)它们是嵌入的XML

<greeting xmlns="http://example.com/customNamespace">Hello!</greeting>

这里假设你使用的是HTML5 doctype <!doctype html >

考虑到这些简单的限制,现在尽最大努力保持HTML标记的有效性是有意义的(请停止关闭<img>和<hr>这样的标记,除非您使用XHTML doctype,否则这是愚蠢和不正确的,您可能不需要XHTML doctype)。

鉴于HTML5清楚地定义了解析规则,兼容的浏览器将能够处理您扔给它的任何标记,即使它不是严格有效的。

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

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

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.

注意:下面的答案在2012年写的时候是正确的。从那以后,事情发生了一些变化。HTML规范现在定义了两种类型的自定义元素——“自主自定义元素”和“自定义内置元素”。前者可以去任何地方措辞内容是预期的;这是主体内的大部分位置,但不是例如ul或ol元素的子元素,或在除td、th或标题元素之外的表元素中。后者可以去任何它们扩展的元素可以去的地方。


这实际上是元素内容模型积累的结果。

例如,根元素必须是html元素。

html元素只能包含head元素和body元素。

The body element may only contain Flow content where flow content is defined as the elements: a, abbr, address, area (if it is a descendant of a map element), article, aside, audio, b, bdi, bdo, blockquote, br, button, canvas, cite, code, command, datalist, del, details, dfn, div dl, em, embed, fieldset, figure, footer, form, h1, h2, h3, h4, h5, h6, header, hgroup, hr, i, iframe, img, input, ins, kbd, keygen, label, map, mark, math, menu, meter, nav, noscript, object, ol, output, p, pre, progress, q, ruby, s, samp, script, section, select, small, span, strong, style (if the scoped attribute is present), sub, sup, svg, table, textarea, time, u, ul, var, video, wbr and Text

等等。

在任何情况下,内容模型都不会说“你可以把任何你喜欢的元素放在这个里面”,这对于自定义元素/标记来说是必要的。