你能在HTML5中定义自己的自闭标签吗? [英] Can you define your own self-closing tag in HTML5?
问题描述
我已阅读此主题,但我还是有疑问。
有没有办法定义void标签?
I have read this topic, but I still have doubts. Is there any way to define void tag?
我试过这个:
<icon class="home"/> (know that slash isn't obligatory)
但是,如果此标签后有任何文字内容FF关闭它们。
But, if there is any text content after this tag FF closes them.
<icon class="home"/> Go home
制作
<icon class="home">Go home</icon>
我应该定义哪个标签是void-element?或者HTML5是不可能的?
Should I define somewhere that tag is void-element? Or is it impossible to do with HTML5?
推荐答案
目前这不是一个答案,因为它没有完全解决这个问题,但是它不适合评论部分。
This is currently not an answer because it does not fully address the question, but it did not fit into the comment section.
/> <>中的
/
如果解析为html5规范,浏览器会忽略/ code>(外部元素
MathLM
除外) SVG
,因为对于 modules 的元素,他们的规范具有自封闭元素,因此它需要保持有效)
The /
in />
is ignored by the browsers if parsed to the html5 specs (except foreign elements
of MathLM
and SVG
, because for the elements of this modules their specs has self enclosing element, so there it needs to stay valid)
规格的相关部分:
- W3C - HMTL5 - Elements
- W3C - HMTL5 - Start tags
(浏览器应如何处理缺失标签以及忽略 /
的相关部分是缺少,我需要查看这个)
(The relevant part how browsers should handle missing tags and that they ignore /
is missing, i need to look this up)
如果元素是 void元素
没有生成结束标记,因为它不需要一个。
If the element is a void element
no closing tag is generated, because it does not require one.
对于其他元素,如果缺少结束标记,则创建结束标记。
所以如果你写这样的东西:
For the other elements the closing tag is created if it is missing. So if you write something like this:
<div>
<div/>test
</div>
这将导致
<div>
<div>test</div>
</div>
因为 /
被忽略。
默认情况下,自定义元素为 non-void
。我知道有一个自定义元素的草稿但老实说,我不知道某些浏览器是否已经支持它。但即使是这样,你也会遇到向后兼容的问题。所以我不建议使用它。
Custom elements are non-void
by default. I know there is a draft for Custom Elements but honestly i don't know if it is already supported in some browsers. But even if it is, you will have the problem of backward compatibility. So i would not recommend to use it.
即使这样定义一个没有前缀 x -
的标签名称,一个坏主意,因为如果后来某个元素被规范添加了您选择的名称,如果它有另一个含义,那么您将遇到问题。
Even so defining a tag name not prefixed with an x-
is a bad idea because if later an element is added by the specs with the name you choose and if that has another meaning you will have a problem.
我有一个查看规格的时间我将提供相应的缺失部分来证明这一点。
As soon as i have time to look up the specs i'll provide the corresponding missing parts to proof this.
这篇关于你能在HTML5中定义自己的自闭标签吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!