IE8 引入三種文檔模式,為兼容性帶來新變數。掌握模式切換與專用 Hack,平滑過渡 IE7→IE8 兼容。
| 模式 | 觸發方式 | 渲染引擎 |
|---|---|---|
| IE8 Standards Mode | 預設(有完整 DOCTYPE) | 嚴格遵循 W3C 標準 |
| IE7 Standards Mode | <meta http-equiv="X-UA-Compatible" content="IE=7"> | IE7 引擎 |
| Quirks Mode | 無 DOCTYPE | IE5 引擎 |
關鍵策略:若現有站點在 IE7 完美運行,不想大改代碼,只需在 <head> 首行加入:
<meta http-equiv="X-UA-Compatible" content="IE=7" />
強制 IE8 用 IE7 模式渲染,零成本兼容。
| Hack | 識別瀏覽器 | 示例 |
|---|---|---|
\9 結尾 | 所有 IE(含 IE8) | margin:0 auto\9; |
*property | IE6/IE7 | *margin:10px; |
_property | 僅 IE6 | _margin:10px; |
<!--[if IE 6]> <link rel="stylesheet" href="ie6.css"> <![endif]-->
<!--[if IE 7]> <link rel="stylesheet" href="ie7.css"> <![endif]-->
<!--[if IE 8]> <link rel="stylesheet" href="ie8.css"> <![endif]-->
<!--[if gt IE 8]><!--> <link rel="stylesheet" href="modern.css"> <!--<![endif]-->
優勢:邏輯清晰、無污染主樣式表、易維護、性能最優(非目標瀏覽器不下載)。
IE8 已停止支援(2016),新專案無需兼容。維護舊專案時,優先用條件註釋分離 IE 樣式,避免 Hack 污染標準代碼。