2012-08-14 108 views
6

我有一个脚本,我想除了低于9 IE版本在每一个浏览器中运行,显然,这个条件语句适用于IE:多个条件语句

<!--[if gt IE 8]> 
    JavaScript stuff here 
<![endif]--> 

但是,代码然后在除IE9以外的任何其他浏览器中不执行。

是否有任何方法一起使用多个条件语句,例如:

<!--[if gt IE 8 OR !IE]> 
    JavaScript stuff here 
<![endif]--> 
+2

你要引用可以在IE8或更低版本中添加一个类,只有在检测到类时才运行脚本。 – elclanrs 2012-08-14 09:08:35

回答

4

你使用这样的条件注释:

<!--[if gte IE 9]><!--> 
    <script></script> 
<!--<![endif]--> 

有两种类型的条件注释:其中整个块被注释掉,其中仅“条件”(这里这个版本)注释掉所有的浏览器(上面的第一个版本)和一的人。

因此,所有非IE浏览器中看到<script></script>外的意见,IE之前V9解析意见和知道忽略HTML。

0

我认为这个页面应该帮助你:

http://www.cssplay.co.uk/menu/conditional.html

具体做法是:

I have found that the following code will allow you to target non-IE browsers 

<!--[if !IE]><!--> 
<h1>You are NOT using Internet Explorer</h1> 
<!--<![endif]--> 

..and finally to target non-IE and a specific IE browser 
(or any combinations using lte lt or gt gte). 

<!--[if IE 6]><!--> 
<h1>You are using EITHER Internet Explorer version 6<br /> 
OR a non-IE browser</h1> 
<!--<![endif]--> 
<h1>You are using 
<!--[if IE 6]>IE6 and not <!--> 
a non-IE browser 
<!--<![endif]--></h1> 
2

这将是最好的,你使用特征检测,而不是条件注释。例如,根据微软的文档中发现here的Internet Explorer 10将完全忽略你的脚本中的任何有条件的意见,例如:通过添加

<!--[if IE]> 
    This content is ignored in IE10. 
    <![endif]--> 

可以修补这个:

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9"> 

但这不是长久之计期限的解决方案。

+0

为什么IE10不再支持条件语句?条件语句真的如此糟糕:S – Sean 2012-08-14 09:48:31

+0

也可以请您解释一下你的特征检测是什么意思?我想我可能会理解,但并非100%确定 – Sean 2012-08-14 09:49:16

+0

微软在我提供的链接上提供了改进互操作性和遵从HTML5的原因。 – Bruno 2012-08-14 09:58:43

2

您可以使用逻辑运算符“加入”条件语句,如[if (gt IE 5)&(lt IE 7)][if (IE 6)|(IE 7)],如here所述。

使用条件语句评论内容为看起来很混乱,我想知道为什么这么多的答案表明这一点。这是我熟悉的符号,与more examples on quirksmode

<!--[if IE]> 
According to the conditional comment this is IE<br /> 
<![endif]--> 
<!--[if !IE]> --> 
According to the conditional comment this is not IE<br /> 
<!-- <![endif]--> 

这句法被推荐in MS specs,更多的功能,如定制版本矢量检测。但请记住,对于所有意图和目的,IE10将作为非IE浏览器检测,并具有作为一个(希望没有更多的显著怪癖,要求在首位的条件语句寻址)