2012-02-02 27 views
30

條件註釋在HAML醫生,我有:「[IE!]」 在Haml的

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

正確的IE(大概在Firefox和Chrome的第一個條件估值,因爲 「這是IE」 不不在這些瀏覽器中呈現)。但是,第二個條件似乎並未在Firefox或Chrome中正確評估,因爲「這不是IE」並未呈現。

我猜我做錯了什麼。有任何想法嗎?

+0

什麼是實際生成的HTML? – 2012-02-02 05:44:24

+0

BronzeGate 2012-02-02 05:46:41

回答

91

使用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

+2

這是一個偉大的答案。我很驚訝你沒有得到更多的讚揚。 +1! – sscirrus 2012-03-05 02:07:12

+4