“[!IE]”哈姆的条件评论 [英] "[ !IE]" conditional comments in Haml

查看:104
本文介绍了“[!IE]”哈姆的条件评论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在HAML文档中,我有:

In a HAML doc, I have:

/[if IE]
  This is IE
/[if !IE]
  This is not IE

第一个条件正确评估IE(可能在Firefox和Chrome中,因为这是IE在这些浏览器中不呈现)。但是,第二个条件似乎没有在Firefox或Chrome中正确评估,因为这不是IE不会呈现。

The first conditional evaluates properly in IE (and presumably in Firefox and Chrome, as "This is IE" does not render in those browsers). However, The second conditional does not seem to evaluate properly in Firefox or Chrome, as "This is not IE" is not rendered.

我猜我已经完成了有问题。任何想法?

I'm guessing I've done something wrong. Any ideas?

推荐答案

使用IE条件评论时,您需要注意两种不同的类型。首先,当整个内容在HTML评论中时(在<! - - > 之间) ,但IE会因为条件而读取它:

When using IE conditional comments, there are two different types you need to be aware of. First, when the entire content is inside a HTML comment (between <!-- and -->), but IE will read it because of the condition:

<!--[if IE]>
  This is inside a HTML comment, so most browsers will ignore it, but IE will
  interpret it.
<![endif]-->

另一种类型是不是一条评论,但有些内容是浏览器会看到,有两条评论会让IE忽略它:

The other type is not a single comment, but some content that browsers will see, surrounded by two comments that will make IE ignore it:

<!--[if !IE]> -->
  This is not a HTML comment, so browsers should see it, but IE will ignore it.
<!-- <![endif]-->

(SO的代码突出显示差异 - 在顶部的一切都是灰色的,因为它是所有评论,但在这一篇文章中,文字较暗,因为它不是评论。)

(SO's code highlighting shows the difference - in the top one everything is grey as it's all comment, but in this one the text is darker as it's not a comment).

Haml对IE条件注释的支持仅对创建第一种类型有用,因为它是创建块注释的语法的一部分。如果您尝试将它用于第二种类型(就像您在这里一样),您会得到类似的结果:

The Haml support for IE conditional comments is only useful for creating the first kind, as it is part of the syntax for creating block comments. If you try using it for the second kind (as you have here) you get something like:

<!--[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] 类型,您可能需要手动执行:

In order to use the [if !IE] type in Haml, you'll probably have to do it manually:

%p Some other content
<!--[if !IE]> -->
%p
  Here's some content that shouldn't appear in IE.
<!-- <![endif]-->

你也可以使用Haml surround helper ,如下所示:

You could also make use of the Haml surround helper, like this:

%p Some other content
=surround '<!--[if !IE]> -->', '<!-- <![endif]-->' do
  %p
    Here's some content that shouldn't appear in IE.

(如果您使用的是Rails,那么您需要使用 html_safe 关于字符串,即环绕'<! - [if!IE]> - >'。html_safe,'<! - <![ endif] - >'。html_safe做)。

(If you’re using Rails then you’ll need to use html_safe on the strings, i.e. surround '<!--[if !IE]> -->'.html_safe, '<!-- <![endif]-->'.html_safe do).

如果你经常使用它,可能值得创建一个帮手将此调用封装到 surround 的方法。

If you're using this a lot, it might be worth creating a helper method that wraps this call to surround.

这篇关于“[!IE]”哈姆的条件评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆