bootstrap3 兼容IE8瀏覽器

EdwardOneil 8年前發布 | 66K 次閱讀 Bootstrap 前端技術

近期在使用bootstrap這個優秀的前端框架,這個框架非常強大,框架里面有下拉菜單、按鈕組、按鈕下拉菜單、導航、導航條、面包屑、分頁、排版、縮略圖、警告對話框、進度條、媒體對象等,bootstrap都已經預先定義好了,當我們制作網頁上,只需直接調用里面的css即可

bootstrap是一個響應式的布局,你可以在寬屏電腦、普通電腦,平板電腦,手機上都得到非常優秀的布局體驗。這種響應式的布局正是通過CSS3的媒體查詢(Media Query)功能實現的,根據不同的分辨率來匹配不同的樣式。IE8瀏覽器并不支持這一優秀的Css3特性,Bootstrap在開發文檔中寫了如何使用進行兼容IE8,如果想兼容IE6,IE7,可以搜索bsie (bootstrap2)

Bootstrap在IE8中肯定不如Chrome、Firefox、IE11那么完美,部分組件不保證完全兼容,還是要Hack的

1、使用html5聲明

<!DOCTYPE html>
這里不可以有空格
<html>

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

2、加入meta標簽

確定顯示此網頁的IE版本

<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 Frame

3、引入bootstrap文件

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

4、引入html5shiv.min.js和respond.min.js

讓不(完全)支持html5的瀏覽器“支持”html5標簽

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

5、添加1.X版本的Jquery庫

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

6、在IE8下測試,發現一個問題placeholder不被支持,下面是解決IE支持placeholder的方法,本文引用的jquery是1.12.0測試通過,先引用jquery

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

也可以用其他的jquery版本,再引入

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

然后在文件中加入一下代碼

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

代碼總結如下:

<!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>

附注:

1、IE下判斷IE版本的語句

<!--[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里的不等于判斷符相同

2、bootstrap3相關css、js

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

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

來自: http://www.cnblogs.com/gamehiboy/p/5147390.html

 本文由用戶 EdwardOneil 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!