HTML條件註釋用法詮釋
註釋內容以樣式爲例,如下:
1、支持所有IE瀏覽器
<!--[if IE]>
<link rel="stylesheet" href="all-ie-only.css" type="text/css"/>
<![endif]-->
2、支持非IE瀏覽器
<!--[if !IE]>
<link rel="stylesheet" href="not-ie.css" type="text/css"/>
<![endif]-->
上面是除了IE瀏覽器外所有瀏覽器都識別這個樣式,另外CSS-TRICKS的《How To Create an IE-Only Stylesheet》一文中提供了另一種寫法:
<!--[if !IE]><!-->
<link rel="stylesheet" type="text/css" href="not-ie.css" />
<!--<![endif]-->
3、僅僅支持IE10
<!--[if IE 10]>
<link rel="stylesheet" type="text/css" href="ie10.css">
<![endif]-->
4、支持IE10以下版本(IE9以及IE9以下版本)
這種方法是樣式表使用在低於IE10的瀏覽器,換句話說除了IE10以外的所有IE版本都將被支持。
<!--[if lt IE 10]>
<link rel="stylesheet" type="text/css" href="ie9-and-down.css">
<![endif]-->
也可以寫成
<!--[if lte IE 9]>
<link rel="stylesheet" type="text/css" href="ie9-and-down.css">
<![endif]-->
前面我們也說過了lt和lte的區別,lt表示小於版本號,不包括條件版本號本身;而lte是小於或等於版本號,包括了版本號自身
。
上面這幾種方法,使用的是低於(lt)和低於或等於(lte)的方法來判斷,我們也可以使用大於gt
和大於或等於gte
達到上面的效果:
5、高於IE9的版本(IE10以及IE10以上版本)
<!--[if gt IE 9]>
<link rel="stylesheet" type="text/css" href="ie10-and-up.css">
<![endif]-->
或
<!--[if gte IE 10]>
<link rel="stylesheet" type="text/css" href="ie10-and-up.css">
<![endif]-->
6、指定多種IE版本
<!--[if (IE 6)|(IE 7)|(IE 8)]>
<link rel="stylesheet" type="text/css" href="ie6-7-8.css">
<![endif]-->