条件注释区分非IE浏览器

IE浏览器的条件注释虽不太常用,却异常强大,不仅可以用来区分IE浏览器版本

<!--[if IE]> Only IE <![endif]-->
所有的IE可识别
<!--[if IE 5.0]> Only IE 5.0 <![endif]-->
只有IE5.0可以识别
<!--[if gt IE 5.0]> Only IE 5.0+ <![endif]-->
高于IE5.0都可以识别
<!--[if lt IE 6]> Only IE 6- <![endif]-->
低于IE6可识别
<!--[if gte IE 6]> Only IE 6/+ <![endif]-->
IE6以及IE6以上都可识别
<!--[if lte IE 7]> Only IE 7/- <![endif]-->
IE7及ie7以下版本可识别

lte:就是Less than or equal to的简写,也就是小于或等于的意思。

lt :就是Less than的简写,也就是小于的意思。

gte:就是Greater than or equal to的简写,也就是大于或等于的意思。

gt :就是Greater than的简写,也就是大于的意思。

! :就是不等于的意思,跟javascript里的不等于判断符相同

 

看到这里只可能会觉得<!--[if !IE]> not IE<![endif]-->这样不就可以了吗?

如果你这样想就错了,因为非ie根本不会识别ie的条件注释,所以就直接全部是注释了,
ok,来看看正确的写法吧

<!--[if !IE]><!-->
只有不是ie浏览器才能看到这里
<!--<![endif]-->
本来准备到这里加点内容只有非ie能看到的,被编辑器编码了,^_^,现在试试吧,只有非ie能看到哦,
关键是条件注释后头的 <!--> 在IE中被当作内部注释,而在非IE浏览器中会闭合之前的注释,从而起到区分非IE浏览器的作用,一般常用<!–>。

From:http://blog.silentash.com/2010/03/conditional-comments-for-ie-and-non-ie-browsers/

 

More:

<!--[if lt IE 7 ]> <html class="ie ie6"> <![endif]-->
<!--[if IE 7 ]>    <html class="ie ie7"> <![endif]-->
<!--[if IE 8 ]>    <html class="ie8"> <![endif]-->
<!--[if IE 9 ]>    <html class="ie9"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html> <!--<![endif]-->

posted @ 2010-03-05 19:07  sohighthesky  阅读(4809)  评论(1编辑  收藏  举报