IE条件注释可以怎么玩

如果玩过条件注释的,可以忽略以下基础介绍。

IE条件注释(Conditional comments)是IE浏览器私有的代码,是一个类似IF判断的语法注释块,IE5之上支持。


代码看起来是这样的

  1. <!--[if IE 6]>
  2. 你正在使用IE6
  3. <![endif]-->
复制代码

他的语法是一个普通的HTML注释 <!– comments –>,分支块以 [if 条件(conditional)]> 开始 <![endif]结束。条件和JS中的if很类似,布尔值类型,可以把浏览器特性作为条件,比如IE ,IE 6, IE 7 ,此外还支持 非(!) 、与(&) 、或(|)、 括号、 大于(gt)、 大于等于(gte)、 小于(le) 、 小于等于(lte)。


直观的代码如下:

  1. <p class="accent">
  2. <!--[if IE]>
  3. According to the conditional comment this is IE<br />
  4. <![endif]-->
  5. <!--[if IE 6]>
  6. According to the conditional comment this is IE 6<br />
  7. <![endif]-->
  8. <!--[if IE 7]>
  9. According to the conditional comment this is IE 7<br />
  10. <![endif]-->
  11. <!--[if IE 8]>
  12. According to the conditional comment this is IE 8<br />
  13. <![endif]-->
  14. <!--[if IE 9]>
  15. According to the conditional comment this is IE 9<br />
  16. <![endif]-->
  17. <!--[if gte IE 8]>
  18. According to the conditional comment this is IE 8 or higher<br />
  19. <![endif]-->
  20. <!--[if lt IE 9]>
  21. According to the conditional comment this is IE lower than 9<br />
  22. <![endif]-->
  23. <!--[if lte IE 7]>
  24. According to the conditional comment this is IE lower or equal to 7<br />
  25. <![endif]-->
  26. <!--[if gt IE 6]>
  27. According to the conditional comment this is IE greater than 6<br />
  28. <![endif]-->
  29. <!--[if !IE]> -->
  30. According to the conditional comment this is not IE<br />
  31. <!-- <![endif]-->
  32. </p>
复制代码

更详细的信息可以在微软MSDN文档中查看

http://msdn.microsoft.com/en-us/library/ms537512.aspx


基础介绍完毕,这个东西可以做IE浏览器检测,所以变成了CSS兼容多版本浏览器的方案之一。


最普遍使用场景1

  1. <!--[if IE 8]>
  2. <link href="ie8.css" rel="stylesheet" type="text/css" media="screen" />
  3. <![endif]-->
  4. <!--[if IE 7]>
  5. <link href="ie7.css" rel="stylesheet" type="text/css" media="screen" />
  6. <![endif]-->
  7. <!--[if lt IE 7]>
  8. <link href="ie7lt.css" rel="stylesheet" type="text/css" media="screen" />
  9. <![endif]-->
复制代码

既可以解决浏览器差异,还可以保证CSS的标准化,避免了很多私有CSS属性作为hack的方式。
可是这样会增加过多的文件加载,维护代码数量也增加,有没有更好的方式?


使用场景2

  1. <!--[if lt IE 7]><html class="ie6 oldie" lang="zh"><![endif]-->
  2. <!--[if IE 7]><html class="ie7 oldie" lang="zh"><![endif]-->
  3. <!--[if IE 8]><html class="ie8 oldie" lang="zh"><![endif]-->
  4. <!--[if gt IE 8]><!--> <html lang="zh"> <!--<![endif]-->
复制代码

场景1中的问题就解决了。通过选择器的优先级就可以轻松解决差异。


有了条件注释,JS也能从总获益,免去的通过JS去判断浏览器类型和版本了。


比如:如果你的页面想使用html5标签,条件注释也能发挥作用。

  1. <!--[if lte IE 8]>
  2. <script>
  3. (function(){
  4.     var e = "abbr,article,aside,audio,canvas,datalist,details,dialog,eventsource,figure,
  5. footer,header,hgroup,mark,menu,meter,nav,output,progress,section,time,video".split(','),
  6.         i= e.length; 
  7.     while(i--){
  8.       document.createElement(e[i]);
  9.     }
  10. })();
  11. </script>
  12. <![endif]-->
复制代码

再比如:IE6的背景图片缓存问题
  1. <!--[if IE 6]>
  2. <script>
  3. document.execCommand("BackgroundImageCache", false, true);
  4. </script>
  5. <![endif]-->
复制代码


甚至还可以帮助JS直接获取浏览器信息,大多数库和方案识别浏览器都是通过userAgent串处理的,而且大部分的应用场景也是if (IExx) {doxx}

  1. function isIE(v){
  2.     var v = v || "",
  3.     tester = document.createElement('div');
  4.     tester.innerHTML = '<!--[if IE ' + v + ']><i></i><![endif]-->';
  5.     return !!tester.getElementsByTagName('i')[0];
  6. }
复制代码

form: http://ninofocus.com/2011/12/09/ie-browse-detect/


还可以在HTML代码中玩

  1. <body>
  2. <!--[if lte IE 8]>
  3. <p>亲爱的用户,您的浏览器版本过低,建议您升级浏览器获得更好的体验....</p>
  4. <![endif]-->
复制代码

或许还有更多的玩法,就等待您的发现和分享了。


最后,条件注释也不仅限于HTML中,JS也可以有,那就是JScript的特性了,这种坑爹的东东还是少用的好,因为JS中的注释总是要被压缩掉的。

  1. <script>
  2. /*@cc_on
  3.   document.write("You are using IE4 or higher");
  4. @*/
  5. </script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值