对于自定义标签在HTML5中是否有效,我一直无法找到一个明确的答案,比如:
<greeting>Hello!</greeting>
我在说明书中什么都没发现:
http://dev.w3.org/html5/spec/single-page.html
而且自定义标记似乎无法使用W3C验证器进行验证。
对于自定义标签在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
等等。
在任何情况下,内容模型都不会说“你可以把任何你喜欢的元素放在这个里面”,这对于自定义元素/标记来说是必要的。
其他回答
注意:下面的答案在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
等等。
在任何情况下,内容模型都不会说“你可以把任何你喜欢的元素放在这个里面”,这对于自定义元素/标记来说是必要的。
我知道这个问题很老了,但我一直在研究这个主题,尽管上面的一些陈述是正确的,但它们不是创建自定义元素的唯一方法。例如:
<button id="find">click me</button>
<Query? ?attach="find" ?content="alert( find() );" ?prov="Hello there :D" >
I won't be displayed :D
</Query?>
<style type="text/css">
[\?content] {
display: none;
}
</style>
<script type="text/javascript">
S = document.getElementsByTagName("Query?")[0];
Q = S.getAttribute("?content");
A = document.getElementById( S.getAttribute("?attach") );
function find() {
return S.getAttribute("?prov");
}
(function() {
A.setAttribute("onclick", Q);
})();
</script>
将工作得非常好(在谷歌Chrome, IE, FireFox和移动Safari的新版本)。您所需要的只是一个alpha字符(a-z, a-z)来开始标记,然后您可以使用任何非alpha字符。如果在CSS中,你必须使用“\”(反斜杠)来查找元素,比如需要Query\^{…}。但在JS中,你只是用你看到的来称呼它。我希望这能帮到你。参见这里的例子
- 水貂CBOS
基本自定义元素和属性
自定义元素和属性在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元素
data-*属性在HTML5中有效,甚至在HTML4中,所有web浏览器都尊重它们。 添加新标签在技术上是可以的,但不建议这样做,因为:
它可能会与将来添加的内容相冲突,而且 使HTML文档无效,除非通过JavaScript动态添加。
我只在谷歌不关心的地方使用自定义标签,在一个游戏引擎iframe中,我做了一个<日志>标签,包含<msg>, <错误>和<警告> -但只通过JavaScript。根据验证器,它是完全有效的。它甚至可以在Internet explorer中使用它的样式!;]
只需使用任何你想要的,而不需要任何dom声明
<container>content here</container>
添加您自己的样式(display:block),它将适用于任何现代浏览器