“自定义元素"是否需要在名称中使用破折号? [英] Do Custom Elements require a dash in their name?

查看:132
本文介绍了“自定义元素"是否需要在名称中使用破折号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在不使用破折号的情况下命名自己的自定义元素<date><person><city>或其他?可以在没有它们的情况下使用定义元素吗?

Is it possible to name your own custom elements <date>, <person>, <city> or others without the use of a dash? Can use define elements without them?

推荐答案

所有浏览器均支持被视为已知"的HTML元素的有限列表.在现代浏览器中,未知元素(例如<city><person>)通常不会在HTML解析器中引发错误,而是从HTMLUnknownElement继承.但是,在较旧版本的IE中,此类元素将作为一个空节点插入DOM,而没有任何子节点(1).

All browsers support a finite list of HTML elements which are considered as "known". Elements which are unknown (e.g <city>, <person>) do not generally throw errors with the HTML parser in modern browsers and instead inherit from HTMLUnknownElement. In older versions of IE however, such elements would be inserted into the DOM as an empty node without any children (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>. These are valid names, whilst <person> is considered an unknown element.

破折号有效地允许HTML解析器分辨出真正的自定义元素和常规元素之间的区别.当标准组将新标签添加到HTML时,它还允许我们启用将来的功能.

The dash effectively allows the HTML parser to tell the difference between true custom elements and regular elements. It also allows us to enable a level of future capability when standards groups add new tags to 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
  • annotation-xml
  • color-profile
  • font-face
  • font-face-src
  • font-face-uri
  • font-face-format
  • font-face-name
  • missing-glyph

据我所知,这些名称是SVG,MathML和其他规范的保留名称.例如,在<font-face>元素上的更多信息.

To the best of my knowledge, these names are reserved names from SVG, MathML and other specs. For example, here's more info on the <font-face> element.

(1)这引起了黑客的攻击,开发人员将使用JavaScript在IE中创建虚拟HTML5标签(例如<article>),以便他们随后可以使用CSS对任何普通元素设置样式.

(1) This gave rise to the hack where developers would create a dummy HTML5 tag in IE (e.g <article>) using JavaScript so that they could then style it per any normal element with CSS.

这篇关于“自定义元素"是否需要在名称中使用破折号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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