条件注释和有效的 XHTML
给定代码(看起来应该是有效的):
<!--[if lt IE 7]> <style type="text/css" media="screen">
<!--
div.stuff { background-image: none; }
--></style><![endif]-->
W3C 验证器抛出一个 fit:
- S 分隔符在注释声明中
- 注释声明
- 无效注释声明:在注释外部找到名称起始字符,但此处不允许
字符数据等等
我不完全确定发生了什么事。 是“嵌套”评论吗? 该标签由 Zend Framework Viewhelper headStyle 生成
$this->headStyle()->prependStyle('div.stuff { background-image: none; }',
array('conditional' => 'lt IE 7')
);
Given the code (which looks like it should be valid):
<!--[if lt IE 7]> <style type="text/css" media="screen">
<!--
div.stuff { background-image: none; }
--></style><![endif]-->
The W3C validator throws a fit:
- S separator in comment declaration
- invalid comment declaration: found name start character outside comment but inside comment declaration
- character data is not allowed here
etc etc
I'm not totally sure whats going on. Is it the 'nested' comments? The tag is being generated by the Zend Framework Viewhelper headStyle
$this->headStyle()->prependStyle('div.stuff { background-image: none; }',
array('conditional' => 'lt IE 7')
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
注释中不能包含
--
,除非它是以有效 XML/XHTML 结尾的-->
的一部分。 这就是评论的工作方式。来自此来源:
您应该找到一种更标准的方法来区分浏览器(或者,更理想的是,拥有根本不需要区分浏览器的布局)。
You can't have a
--
inside of a comment unless it's part of the-->
ending in valid XML/XHTML. Just the way comments work.From this source:
You should find a more standard way to differentiate between browsers (or, more ideally, have a layout that doesn't require differentiation between browsers at all).
“
-->
”关闭任何注释,不存在相互嵌套注释的概念。 因此,在您的代码中,第一个“-->
”会关闭您的两条注释。 那么完全在任何注释之外,所以没有任何意义。
"
-->
" closes any comment, there is no notion of nesting comments inside each other. So in your code, the first "-->
" closes both of your comments. Then the<![endif]-->
is completely outside of any comments, so doesn't make any sense.这是嵌套的注释。 他们是不被允许的。
It is the nested comments. They are not allowed.
...为什么要注释掉
的全部内容? 这并不是说您正在为一个愚蠢到足以显示它的浏览器进行编码。 (甚至命令行浏览器也会隐藏样式/脚本块。)
编辑:啊,等等。 这是由 Zend 生成的。
...and why comment out the entire contents of
<style>
? It's not as if you're coding for a browser that is dumb enough to display it. (Even command-line browsers hide the style/script blocks.)Edit: Ah, wait. That's generated by Zend.
您应该在问题跟踪器上发布新问题。 这是纠正此类错误的好方法。
http://framework.zend.com/issues/secure/Dashboard.jspa
You should post new issue on issue tracker. It's a good way to make such mistakes corrected.
http://framework.zend.com/issues/secure/Dashboard.jspa
Phil Booth 给出的答案是正确的,因为您的 HTML 注释语法不正确; HTML 注释不能嵌套。 然而,我想更进一步...
您不应该使用 HTML 注释来隐藏您的 CSS 或 JavaScript,以防止 XHTML 验证。 相反,您应该使用
CDATA
标记。 这是最通用的解决方案,支持几乎所有浏览器和新旧浏览器版本。这些文章更详细地介绍了为什么上述解决方案是正确的:
The answer given by Phil Booth is correct in that your HTML comment syntax is incorrect; HTML comments cannot be nested. However, I'd like to take it a step further...
You should not use HTML comments to hide your CSS or JavaScript from XHTML validation. Instead, you should use
CDATA
tags. This is the most universal solution, supporting pretty much every browser and browser version new and old.These articles go into more detail about why the aforementioned solution is the correct one: