本文介绍了HTML注释行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我注意到当在Magento所见即所得的编辑器(所有事物)中乱搞时,<!> 呈现为< ;! ----> 在结果HTML中。它似乎也包含任何字符串在< ;! > 呈现正常的<! - [string] - > 评论。我只在Chrome中测试过,但这种行为对我来说似乎有点奇怪。我已阅读 ,以及MDN上的小文件。尽管如此,我也没有给我一个答案,我可以真正包裹我的头。 注释open delimiter( - )有什么意义,如果省略,<!> 似乎总是导致一个适当的评论?也许,我的浏览器是否摆脱了无效标记?为什么不是<!> 只是呈现为文本?我知道这并不重要,甚至根本不关心,但我只是好奇!

解决方案

正式来说,HTML到HTML 4.01的注释语法很复杂,遵循SGML标准,但实际上HTML从未作为SGML应用程序实现。 WDG的旧页面简要解释了HTML注释语法,并使关于它的一些实际笔记。实际上,评论应以<! - 开始,并以 - > 结尾。这在XHTML和HTML5中是正式的。 但是浏览器已经更加宽松地实现了注释语法,所以你可以用<!来启动它们,并以> (尽管这不是官方的,并且会导致来自HTML5验证器的错误消息)。这已经在HTML5解析规则中描述并规定为所需的浏览器行为,请参见第8.2.4.45节,它定义了如何输入假评论状态。

因此,< ;! foo> 实际上可以工作(并在DOM中创建一个注释节点),但它在XHTML和HTML5中被禁止。


So, I noticed while messing around in a Magento WYSIWYG editor (of all things) that <!> renders as <!----> in the resultant HTML. It also seems that enclosing any string in <! > renders a normal <!-- [string] --> comment. I only tested this in Chrome, but this behavior seems a little odd to me. I have looked at the W3C spec on comments, as well as this little document on the MDN. Neither gave me an answer I could really wrap my head around, though; what is the significance of the comment open delimiter ("--"), and if it is left out, how does <!> seem to always result in a proper comment? Is it, perhaps, my browser just getting rid of invalid markup? Why isn't <!> just rendered as text? I know this isn't important, or even pertinent to anything at all, but I am just curious!

解决方案

Formally, the comment syntax in HTML up to HTML 4.01 is complicated, following the SGML standard, but in practice HTML was never implemented as an SGML application. An old page HTML comments by WDG briefly explains the HTML comment syntax and makes some practical notes on it. In practice, a comment should start with <!-- and end with -->. This is made formal in XHTML and in HTML5.

But browsers have implemented comment syntax more liberally, so that you can start them with <! and end with > (even though this is not official and causes an error message from an HTML5 validator). This has been described and prescribed as required browser behavior in HTML5 parsing rules, see section 8.2.4.45 Markup declaration open state, which defines how "bogus comment state" will be entered.

Thus, <! foo > actually works (and creates a comment node in the DOM), but it is forbidden in XHTML and in HTML5.

这篇关于HTML注释行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 13:12