internet-explorer - Haml中的“[ !IE]”条件注释

标签 internet-explorer conditional haml

在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/

相关文章:

ruby - 将 JS 变量注入(inject) haml 模板

ruby-on-rails - 如何在字段旁边显示错误消息

html - 在 TD 内创建高度为 100% 的 div

angular - 在 Angular 2 中应用条件样式

css - "unbound"SELECT 元素的选项元素的 knockout 条件样式

php - 为什么使用 !== FALSE 来检查 php 中的 stripos?

ruby-on-rails - 使用 Ruby HAML 的空属性

windows - 从 SHDocVw WebBrowser 控件中检索 "highest level frame"

css - 在 IE 8 上无法点击背景图片上的链接/鼠标

javascript - 项目中放置带有 polyfill 文件以支持跨浏览器 JavaScript 的最佳位置