“[!IE]"Haml 中的条件注释 [英] "[ !IE]" conditional comments in Haml

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

问题描述

在 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).

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

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]-->

这实际上是一个无条件的评论.

which is effectively an unconditional comment.

为了在 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助手,像这样:

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,即 surround '<!--[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]"Haml 中的条件注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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