随机字母 html 标签

Random Letter html Tag

我想知道您是否可以使用随机字母作为 html 标签。比如,f 不是标签,但我在一些代码中尝试过它,它就像一个 span 标签一样工作。对不起,如果这是一个糟糕的问题,我只是好奇了一段时间,但我在网上找不到任何东西。

I was wondering if you can use a random letter as an html tag.

是也不是。

“是”- 它有效,但它不正确:当你有类似 <z> 它之所以有效,是因为网络 (HTML+CSS+JS) 具有一定程度的 向前兼容性 内置:浏览器将呈现 HTML他们无法识别的元素与 <span> 基本相同(即 inline 元素除了 reify 文档文本范围之外什么都不做)。

但是,to use HTML5 Custom Elements correctly 您需要遵守自定义元素规范,其中规定:

The name of a custom element must contain a dash (-). So <x-tags>, <my-element>, and <my-awesome-app> are all valid names, while <tabs> and <foo_bar> are not. This requirement is so the HTML parser can distinguish custom elements from regular elements. It also ensures forward compatibility when new tags are added to HTML.

所以如果你使用 <my-z> 就没问题了。


The HTML Living Standard document, as of 2021-12-04,确实在其自定义元素名称要求列表中明确引用了向前兼容性:

https://html.spec.whatwg.org/#valid-custom-element-name

  • They start with an ASCII lower alpha, ensuring that the HTML parser will treat them as tags instead of as text.
  • They do not contain any ASCII upper alphas, ensuring that the user agent can always treat HTML elements ASCII-case-insensitively.
  • They contain a hyphen, used for namespacing and to ensure forward compatibility (since no elements will be added to HTML, SVG, or MathML with hyphen-containing local names in the future).
  • They can always be created with createElement() and createElementNS(), which have restrictions that go beyond the parser's.
  • Apart from these restrictions, a large variety of names is allowed, to give maximum flexibility for use cases like <math-α> or <emotion->.

因此,举个例子:

  • <a><q><b><i><u><p><s>
    • :这些单字母元素已被HTML使用。
  • <z>
    • :不包含连字符 - 的元素名称不能是自定义元素,现代浏览器会将其解释为 invalid/unrecognized标记,他们仍然(大部分)将其视为 <span> 元素。
  • <a:z>
    • :在HTML中使用冒号来使用XML元素命名空间不是一回事 5 除非你使用 XHTML5.
  • <-z>
    • - 元素名称必须以从 az 的小写 ASCII 字符开头,因此不允许 -
  • <a-z>
    • 是的 - 这很好。
  • <a-><a-->
    • 不确定——这两个名字很好奇:
      • HTML 规范规定名称必须符合语法规则 [a-z] (PCENChar)* '-' (PCENChar)*
        • * 表示“零个或多个”,这是奇数,因为这意味着连字符 不需要 后跟另一个字符。
        • PCENChar 表示元素名称中允许的大量可见字符,奇怪的是这包括 -,因此根据该规则 <a--> 应该 有效。
          • 但请注意,-- 是更大的 SGML 家族(包括 HTML 和 XML)中的保留字符序列,这可能会导致奇怪。 YMMV!