DIV CSS 佈局教程網

讓 Bootstrap 3 在 IE8 下正常運作:完整設定步驟
編輯:關於JavaScript     

近來在用 Bootstrap 這個優秀的前端框架。它幾乎把常用元件都預先定義好了——下拉選單、按鈕組、導覽列、麵包屑、分頁、排版、縮圖、警告框、進度條、媒體物件等等,做頁面時直接套用它的 class 即可。

Bootstrap 是響應式設計的,寬螢幕電腦、普通電腦、平板、手機上都能得到合適的版面,而這靠的是 CSS3 的媒體查詢(Media Query)依解析度匹配不同樣式。問題就在這裡:IE8 不支援媒體查詢。Bootstrap 官方文件因此寫了 IE8 的相容做法;若要相容 IE6/IE7,可以找 bsie(對應 Bootstrap 2)。

先有心理準備:Bootstrap 在 IE8 中的呈現一定不如 Chrome、Firefox、IE11 完美,部分元件不保證完全一致,多少還是要 hack。

1. 使用 HTML5 語法宣告

<!DOCTYPE html>
<html>

注意:寫成 HTML 4.01 的 Strict DTD 宣告是行不通的。

2. 加入 meta 標籤

用來決定以哪個版本的核心來渲染:

<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 表示強制使用該瀏覽器最新的核心;chrome=1 則表示若使用者安裝了 Google Chrome Frame 這個外掛,就用 Chrome 的核心來渲染。

3. 引入 Bootstrap 樣式檔

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

4. 引入 html5shiv 與 respond.js

前者讓不支援 HTML5 的瀏覽器認得新標籤,後者則是為 IE8 補上媒體查詢的能力。

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

Bootstrap 3 需要 jQuery,且不支援 jQuery 2.x 以上——因為 2.x 已經放棄 IE8。

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

6. 補上 placeholder 支援

在 IE8 下實測會發現 placeholder 完全無效。以下寫法以 jQuery 1.12.0 測試通過:

<script type="text/javascript" src="js/bootstrap/jquery-1.12.0.min.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>
<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/html5shiv.min.js"></script>
<script src="js/bootstrap/respond.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 條件式註解語法

<!--[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 的判斷符號相同。

最後補一句現實狀況:微軟已於 2016 年停止支援 IE11 之前的版本,IE 整體也在 2022 年走入終結。今天若還有 IE8 相容需求,通常代表專案背後有無法升級的企業內部系統。若你正評估是否該繼續投入這條路,值得先確認一下:多數情況下,與其為 IE8 做無止境的 hack,不如對這些使用者提供一個「請改用現代瀏覽器」的提示頁,並把開發精力放在真正的主流環境上。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved