【兼容】兼容IE8浏览器

bootstrap3 兼容IE8浏览器

  近期在使用bootstrap这个优秀的前端框架,这个框架很是强大,框架里面有下拉菜单、按钮组、按钮下拉菜单、导航、导航条、面包屑、分页、排版、缩略图、警告对话框、进度条、媒体对象等,bootstrap都已经预先定义好了,当咱们制做网页上,只需直接调用里面的css便可javascript

  bootstrap是一个响应式的布局,你能够在宽屏电脑、普通电脑,平板电脑,手机上都获得很是优秀的布局体验。这种响应式的布局正是经过CSS3的媒体查询(Media Query)功能实现的,根据不一样的分辨率来匹配不一样的样式。IE8浏览器并不支持这一优秀的Css3特性,Bootstrap在开发文档中写了如何使用进行兼容IE8,若是想兼容IE6,IE7,能够搜索bsie (bootstrap2)css

  Bootstrap在IE8中确定不如Chrome、Firefox、IE11那么完美,部分组件不保证彻底兼容,仍是要Hack的html

一、使用html5声明前端

1
2
3
<! DOCTYPE  html>
这里不能够有空格
< html >

注:写成<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">是不可行的html5

二、加入meta标签java

肯定显示此网页的IE版本jquery

1
2
< meta  http-equiv="X-UA-Compatible" content="IE=edge,Chrome=1" />
< meta  http-equiv="X-UA-Compatible" content="IE=9" />

注:bootstrap不支持IE兼容模式,为了让IE浏览器运行最新的渲染模式,将添加以上标签在页面中,IE=edge表示强制使用IE最新内核,chrome=1表示若是安装了针对IE6/7/8等版本的浏览器插件Google Chrome Framegit

三、引入bootstrap文件github

1
< link  href="css/bootstrap/bootstrap.min.css" rel="stylesheet">

四、引入html5shiv.min.js和respond.min.jschrome

   让不(彻底)支持html5的浏览器“支持”html5标签

1
2
3
4
<!--[if lt IE 9]>
<script src="js/bootstrap/html5shiv.min.js"></script>
<script src="js/bootstrap/respond.min.js"></script>
<![endif]-->

五、添加1.X版本的Jquery库

1
< script  src="js/bootstrap/jquery-1.12.0.min.js"></ script >

六、在IE8下测试,发现一个问题placeholder不被支持,下面是解决IE支持placeholder的方法,本文引用的jquery是1.12.0测试经过,先引用jquery

1
2
< script  type="text/javascript" src="js/bootstrap/jquery-1.12.0.min.js"></ script >
< script  src="bootstrap/js/bootstrap.min.js"></ script >

也能够用其余的jquery版本,再引入

1
< script  type="text/javascript" src="js/bootstrap/jquery.placeholder.js"></ script >

而后在文件中加入一下代码

1
2
3
4
5
< script  type="text/javascript">
     $(function () {
         $('input, textarea').placeholder();
     });
</ script >

代码总结以下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!DOCTYPE html>
<html lang= "zh-CN" >
<head>
     <meta charset= "UTF-8" >
     <meta name= "viewport"  content= "width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" >
     <meta http-equiv= "X-UA-Compatible"  content= "IE=edge,Chrome=1"  />
     <meta name= "author"  content= "zhy"  />
     <title>ie8</title>
     <link rel= "stylesheet"  href= "css/bootstrap/bootstrap.min.css" >
     <!--[ if  lte IE  9 ]>
     <script src=js/bootstrap/respond.min.js"></script>
     <script src=js/bootstrap/html5shiv.min.js"></script>
     <![endif]-->
     <script src= "js/bootstrap/jquery-1.12.0.min.js" ></script>
     <script src= "js/bootstrap/bootstrap.min.js" ></script>
</head>
<body>
</body>
</html>

附注:

一、IE下判断IE版本的语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!--[if lte IE 6]>
<![endif]-->
IE6及其如下版本可见
 
<!--[if lte IE 7]>
<![endif]-->
IE7及其如下版本可见
 
<!--[if IE 6]>
<![endif]-->
只有IE6版本可见
 
<![if !IE]>
<![endif]>
除了IE之外的版本
 
<!--[if lt IE 8]>
<![endif]-->
IE8如下的版本可见
 
<!--[if gte IE 7]>
<![endif]-->
IE7及大于IE7的版本可见

lte:就是Less than or equal to的简写,也就是小于或等于的意思。
lt :就是Less than的简写,也就是小于的意思。
gte:就是Greater than or equal to的简写,也就是大于或等于的意思。
gt :就是Greater than的简写,也就是大于的意思。
! : 就是不等于的意思,跟javascript里的不等于判断符相同

二、bootstrap3相关css、js

   下载地址:http://pan.baidu.com/s/1getpDjt

     jquery.placeholder.js文件的下载地址https://github.com/mathiasbynens/jquery-placeholder