Why is Stylesheet loaded when Contional Comment states it should be ignored?

风流意气都作罢 提交于 2019-12-08 06:01:27

问题


I thought conditional comments would instruct the browser to ignore the content if the condition is not met?!

For example I want to only include a stylesheet if IE6 is the browser. The following in located in the <HEAD> element of the page.

<!--[if IE 6]>
  <link id="IE6StyleSheet" rel="Stylesheet" type="text/css" href="~/css/IE6.css" runat="server" />
<![endif]-->

or

<!--[if IE 6]>
  <link rel="Stylesheet" type="text/css" href="../css/IE6.css" />
<![endif]-->

How come IE7, IE8 and FF3 all load that stylesheet?!

NOTE: Changing the condition to [if lte IE 6] does not make any difference! :(

MAJOR UPDATE

I am a moron... I just noticed what I did wrong! The example I'd given was slightly modified. The path to the css file in under App_Themes! Of course the css was always loaded!!!


回答1:


Try:

<!--[if lte IE 6]>
   <link id="IE6StyleSheet" rel="Stylesheet" type="text/css" href="../css/IE6.css" />
<![endif]-->

This will only load the stylesheet for IE6 or lower versions. Here's a test script you can use, it will print out which version of IE you're using:

<p><!--[if IE]>
According to the conditional comment this is Internet Explorer<br />
<![endif]-->
<!--[if IE 5]>
According to the conditional comment this is Internet Explorer 5<br />
<![endif]-->
<!--[if IE 5.0]>
According to the conditional comment this is Internet Explorer 5.0<br />
<![endif]-->
<!--[if IE 5.5]>
According to the conditional comment this is Internet Explorer 5.5<br />
<![endif]-->
<!--[if IE 6]>
According to the conditional comment this is Internet Explorer 6<br />
<![endif]-->
<!--[if IE 7]>
According to the conditional comment this is Internet Explorer 7<br />
<![endif]-->
<!--[if gte IE 5]>
According to the conditional comment this is Internet Explorer 5 and up<br />
<![endif]-->
<!--[if lt IE 6]>
According to the conditional comment this is Internet Explorer lower than 6<br />
<![endif]-->
<!--[if lte IE 5.5]>
According to the conditional comment this is Internet Explorer lower or equal to 5.5<br />
<![endif]-->
<!--[if gt IE 6]>
According to the conditional comment this is Internet Explorer greater than 6<br />
<![endif]-->
</p>

You should not see any text in Firefox with this test code.



来源:https://stackoverflow.com/questions/795267/why-is-stylesheet-loaded-when-contional-comment-states-it-should-be-ignored

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!