来自 SGML/HTML 标准:
“简单来说,评论开头和结尾的双破折号并不是评论的开始和结束。双破折号表示评论允许包含的内容发生变化。第一个 -- 开始评论,并告诉浏览器注释允许包含 > 字符而不结束注释。第二个 -- 不结束注释。它告诉浏览器如果遇到 > 字符,则必须结束注释。如果另一个 -添加 - ,然后返回允许 > 字符。 ”
我的问题是:
假设一些 javascript 位于 html 注释中,我将如何处理 '>' 字符 脚本..
例如
<!--
<script>
for(x=10;x>1;x--)
{
if(x>5) {}
}
</script>
-->
难道 x-- 不会将 html 解析器设置为将下一个“>”视为注释的结尾吗? 在这种情况下,哪个是 (x>5) 中的大于号?
一个让我更加困惑的例子:
<!--[if lt IE 9]>
<!--<script type="text/javascript" src="/respond.js" >
var i=10;
if(i-->5) {}
</script>-->
<![endif]-->
最佳答案
根据MDN Documentation on correct use of comments
[15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'
因此 -->
不会在 XML 中结束注释 block ,而是在 HTML 中结束。
来自同一来源的示例
有效评论
<!-- valid xml/xhtml comment -->
无效评论
<!-- invalid -- xml comment -->
<!-- invalid xml comment --->
<!-- invalid xml comment -- >
现在,你的问题
wouldn't the x-- set the html parser to consider the next '>' as the end of the comment
回答:不,html 解析仅将 -->
视为注释的结尾,而不是任何其他组合。 HTML 解析器的结束 block 中只能出现空格,例如 -->
关于javascript - html 解析包含 javascript 的注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22433240/