在HAML文档中,我有:
/[if IE]
This is IE
/[if !IE]
This is not IE
第一个条件在IE中正确评估(大概在Firefox和Chrome中,因为“这是IE”无法在那些浏览器中呈现)。但是,第二个条件在Firefox或Chrome中似乎无法正确评估,因为未呈现“This is IE”。
我猜我做错了什么。有任何想法吗?
最佳答案
使用IE条件注释时,需要注意两种不同的类型。首先,当整个内容位于HTML注释内(<!--
和-->
之间)时,但IE会由于以下条件而读取它:
<!--[if IE]>
This is inside a HTML comment, so most browsers will ignore it, but IE will
interpret it.
<![endif]-->
另一种类型不是单个注释,而是浏览器将看到的某些内容,周围是两个注释,这些注释将使IE忽略它:
<!--[if !IE]> -->
This is not a HTML comment, so browsers should see it, but IE will ignore it.
<!-- <![endif]-->
(SO的代码突出显示了差异-在最上面的一个中,所有内容均为灰色,因此均为注释,但在此文本中,其内容较暗,因为它不是注释)。
The Haml support for IE conditional comments仅对创建第一种有用,因为它是创建块注释的语法的一部分。如果尝试将其用于第二种(如此处所示),则会得到类似以下内容的信息:
<!--[if !IE]>
This is inside a HTML comment, so other browsers will ignore it.
IE will also ignore it, as the conditional states !IE.
So everything ignores it.
<![endif]-->
这实际上是无条件的评论。
为了在Haml中使用
[if !IE]
类型,您可能必须手动进行操作:%p Some other content
<!--[if !IE]> -->
%p
Here's some content that shouldn't appear in IE.
<!-- <![endif]-->
您还可以使用Haml
surround
helper,如下所示:%p Some other content
=surround '<!--[if !IE]> -->', '<!-- <![endif]-->' do
%p
Here's some content that shouldn't appear in IE.
(如果您使用的是Rails,则需要在字符串上使用
html_safe
,即surround '<!--[if !IE]> -->'.html_safe, '<!-- <![endif]-->'.html_safe do
)。如果您经常使用此方法,则可能值得创建一个将此调用包装到
surround
的辅助方法。
关于internet-explorer - Haml中的“[ !IE]”条件注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9107681/