用条件注释判断浏览器版本解决页面兼容问题

2020-05-01 10:03:25易采站长站整理

如我们需要分别兼容IE6、7、8,我们可以这样做


<!–[if IE 8]>
<link rel=”stylesheet” type=”text/css” href=”ie8.cdd”>
<![endif]–>
<!–[if IE 7]>
<link rel=”stylesheet” type=”text/css” href=”ie7.cdd”>
<![endif]–>
<!–[if IE 6]>
<link rel=”stylesheet” type=”text/css” href=”ie6.cdd”>
<![endif]–>

方法2、根据不同的浏览器版本,给html或body挂载不同的类如


<!–[if lt IE 7 ]><html class=”ie6″ lang=”zh-cn”><![endif]–>
<!–[if IE 7 ]><html class=”ie7″ lang=”zh-cn”><![endif]–>
<!–[if IE 8 ]><html class=”ie8″ lang=”zh-cn”><![endif]–>
<!–[if IE 9 ]><html class=”ie9″ lang=”zh-cn”><![endif]–>

最后我们看一下lt,lte,gt,gte分别表示什么

lt:小于当前版本

lte:小于或等于当前版本,包括本身

gt:大于当前版本

gte:大于或等于当前版本,包括本身

好了,我们今天就到这里,大家可能已经知道怎么简单的处理浏览器兼容了,那么下次我们接着介绍一些CSS Hack技术,更进一步的处理浏览器兼容

测试代码:


<!DOCTYPE html>
<html>
<head>
<title> 用条件注释判断浏览器版本,解决兼容问题 </title>
<meta charset=”utf-8″/>
</head>
<body>
<!–[if IE]>只有IE6,7,8,9浏览器显示(IE10标准模式不支持)<hr/><![endif]–>
<!–[if !IE]><!–>只有非IE浏览器显示(不包括IE10)<hr/><!–><![endif]–>
<!–[if IE 9]>IE9浏览器显示<hr/><![endif]–>
<!–[if IE 8]>IE8浏览器显示<hr/><![endif]–>
<!–[if IE 7]>IE7浏览器显示<hr/><![endif]–>
<!–[if IE 6]>IE6浏览器显示<hr/><![endif]–>
<!–[if lt IE 10]>IE10以下版本浏览器显示(不包括IE10)<hr/><![endif]–>
<!–[if lte IE 9]>IE9及IE9以下版本浏览器显示(包括IE9)<hr/><![endif]–>
<!–[if gt IE 6]>IE6以上版本浏览器显示(不含IE6)<hr/><![endif]–>
<!–[if gte IE 7]>IE7及IE7以上版本浏览器显示(包含IE7)<hr/><![endif]–>
</body>
</html>