怎样在HTML代码里面判断IE版本?

由于IE浏览器在处理前端的某些地方和其他浏览器不一致,需要进行特殊处理。因此,需要我们判断当前浏览器类型以及版本,那么我们该怎样在HTML中判断当前是IE浏览器,以及是IE的哪个版本。常见的处理方式有,通过JavaScript代码根据userAgent字符串来判断浏览器的信息;但是关于IE方面,IE在IE5.0以上提供了一种关于注释的语法来判断IE的版本。下面将详细讲解

由于IE浏览器在处理前端的某些地方和其他浏览器不一致,需要进行特殊处理。因此,需要我们判断当前浏览器类型以及版本,那么我们该怎样在HTML中判断当前是IE浏览器,以及是IE的哪个版本。常见的处理方式有,通过JavaScript代码根据userAgent字符串来判断浏览器的信息;但是关于IE方面,IE在IE5.0以上提供了一种关于注释的语法来判断IE的版本。下面将详细讲解

如何让静态HTML代码根据不同IE版本显示不同内容。 这里的技巧就是利用IE的HTML注释表达式。注释格式是

<!-- Comment content  -->

IE 对HTML注释做了一些扩展,使之可以支持条件判断表达式:

<!--[if expression]> HTML <![endif]-->

当表达式expression 为True 的时候,显示 HTML 内容。

例子:如果当前浏览器是IE5,则显示<p>Welcome to Internet Explorer 5.</p>的内容。如下:

<!--[if IE 5]>
<p>Welcome to Internet Explorer 5.</p>
<![endif]-->

和编程语言类似,这里的表达式还支持大于(gt)、小于(lt)、 与或非 等操作符。 下面是一些例子。

[if IE]   判断是否IE
[if IE 7]  判断是否是IE7
[if !IE]  判断是否不是IE
[if lt IE 5.5]  判断是否是IE5.5 以下版本。  (<)
[if lte IE 6]   判断是否等于IE6 版本或者以下 (<=)
[if gt IE 5]  判断是否IE5以上版本  (> )
[if gte IE 7]  判断是否 IE7 版本或者以上
[if !(IE 7)] 判断是否不是IE7
[if (gt IE 5)&(lt IE 7)]   判断是否大于IE5, 小于IE7
[if (IE 6)|(IE 7)]  判断是否IE6 或者 IE7

代码示例:

<!--[if IE]><p>You are using Internet Explorer.</p><![endif]-->  
<![if !IE]><p>You are not using Internet Explorer.</p><![endif]>  
  
<!--[if IE 7]><p>Welcome to Internet Explorer 7!</p><![endif]-->  
<!--[if !(IE 7)]><p>You are not using version 7.</p><![endif]-->  
  
<!--[if gte IE 7]><p>You are using IE 7 or greater.</p><![endif]-->  
<!--[if (IE 5)]><p>You are using IE 5 (any version).</p><![endif]-->  
<!--[if (gte IE 5.5)&(lt IE 7)]><p>You are using IE 5.5 or IE 6.</p><![endif]-->  
<!--[if lt IE 5.5]><p>Please upgrade your version of Internet Explorer.</p><![endif]-->  
<!--[if IE]><p>You are using Internet Explorer.</p><![endif]-->
<![if !IE]><p>You are not using Internet Explorer.</p><![endif]>
<!--[if IE 7]><p>Welcome to Internet Explorer 7!</p><![endif]-->
<!--[if !(IE 7)]><p>You are not using version 7.</p><![endif]-->
<!--[if gte IE 7]><p>You are using IE 7 or greater.</p><![endif]-->
<!--[if (IE 5)]><p>You are using IE 5 (any version).</p><![endif]-->
<!--[if (gte IE 5.5)&(lt IE 7)]><p>You are using IE 5.5 or IE 6.</p><![endif]-->
<!--[if lt IE 5.5]><p>Please upgrade your version of Internet Explorer.</p><![endif]-->

注:IE5 以下的版本不支持这种注释扩展。

睡眠和休息丧失了时间,却取得了明天工作的精力。 —— 毛泽东
0 不喜欢
说说我的看法 -
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号