简体   繁体   English

自定义元素的名称是否需要短划线?

[英]Do Custom Elements require a dash in their name?

Is it possible to name your own custom elements <date> , <person> , <city> or others without the use of a dash? 是否可以在不使用破折号的情况下命名您自己的自定义元素<date><person><city>或其他元素? Can use define elements without them? 没有它们可以使用定义元素吗?

All browsers support a finite list of HTML elements which are considered as "known". 所有浏览器都支持有限的HTML元素列表,这些元素被认为是“已知的”。 Elements which are unknown (eg <city> , <person> ) do not generally throw errors with the HTML parser in modern browsers and instead inherit from HTMLUnknownElement . 未知的元素(例如<city><person> )通常不会在现代浏览器中使用HTML解析器抛出错误,而是从HTMLUnknownElement继承。 In older versions of IE however, such elements would be inserted into the DOM as an empty node without any children (1). 然而,在IE的旧版本中,这些元素将作为空节点插入DOM而没有任何子节点(1)。

The Custom Elements specification requires that all custom elements contain a hyphen ( - ) in the name. 自定义元素规范要求所有自定义元素在名称中包含连字符( - )。 So rather than <person> , you would use <my-person> or <x-person> . 因此,不是<person> ,而是使用<my-person><x-person> These are valid names, whilst <person > is considered an unknown element. 这些是有效名称,而<person >被认为是未知元素。

The dash effectively allows the HTML parser to tell the difference between true custom elements and regular elements. 破折号有效地允许HTML解析器区分真正的自定义元素和常规元素。 It also allows us to enable a level of future capability when standards groups add new tags to HTML. 它还允许我们在标准组向HTML添加新标签时启用未来级别的功能。

You can use any hyphen-separated name with the exception of: 您可以使用任何以连字符分隔的名称,但以下情况除外:

  • annotation-xml
  • color-profile
  • font-face
  • font-face-src
  • font-face-uri
  • font-face-format
  • font-face-name
  • missing-glyph

To the best of my knowledge, these names are reserved names from SVG, MathML and other specs. 据我所知,这些名称是SVG,MathML和其他规范的保留名称。 For example, here's more info on the <font-face> element. 例如,这里有关于<font-face>元素的更多信息

(1) This gave rise to the hack where developers would create a dummy HTML5 tag in IE (eg <article> ) using JavaScript so that they could then style it per any normal element with CSS. (1)这引起了开发人员使用JavaScript在IE中创建虚拟HTML5标签(例如<article> )的黑客攻击,以便他们可以使用CSS为任何普通元素设置样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM