IE條件註釋可以怎麼玩

IE條件註釋可以怎麼玩


如果玩過條件註釋的,可以忽略以下基礎介紹。


IE條件註釋(Conditional comments)是IE瀏覽器私有的代碼,是一個類似IF判斷的語法註釋塊,IE5之上支持。


代碼看起來是這樣的


!--[if IE 6]>


你正在使用IE6


![endif]-->複製代碼


他的語法是一個普通的HTML註釋 !– comments –>,分支塊以 [if 條件(conditional)]> 開始 ![endif]結束。條件和JS中的if很類似,布爾值類型,可以把瀏覽器特性作爲條件,比如IE ,IE 6, IE 7 ,此外還支持 非(!) 、與(&) 、或(|)、 括號、 大於(gt)、 大於等於(gte)、 小於(le) 、 小於等於(lte)。


直觀的代碼如下:


p class="accent">


!--[if IE]>


According to the conditional comment this is IEbr />


![endif]-->


!--[if IE 6]>


According to the conditional comment this is IE 6br />


![endif]-->


!--[if IE 7]>


According to the conditional comment this is IE 7br />


![endif]-->


!--[if IE 8]>


According to the conditional comment this is IE 8br />


![endif]-->


!--[if IE 9]>


According to the conditional comment this is IE 9br />


![endif]-->


!--[if gte IE 8]>


According to the conditional comment this is IE 8 or higherbr />


![endif]-->


!--[if lt IE 9]>


According to the conditional comment this is IE lower than 9br />


![endif]-->


!--[if lte IE 7]>


According to the conditional comment this is IE lower or equal to 7br />


![endif]-->


!--[if gt IE 6]>


According to the conditional comment this is IE greater than 6br />


![endif]-->


!--[if !IE]> -->


According to the conditional comment this is not IEbr />


!-- ![endif]-->


/p>複製代碼


更詳細的信息可以在微軟MSDN文檔中查看


基礎介紹完畢,這個東西可以做IE瀏覽器檢測,所以變成了CSS兼容多版本瀏覽器的方案之一。


最普遍使用場景1


!--[if IE 8]>


link href" rel="stylesheet" type="text/css" media="screen" />


![endif]-->


!--[if IE 7]>


link hr" rel="stylesheet" type="text/css" media="screen" />


![endif]-->


!--[if lt IE 7]>


link href="ss" rel="stylesheet" type="text/css" media="screen" />


![endif]-->複製代碼


既可以解決瀏覽器差異,還可以保證CSS的標準化,避免了很多私有CSS屬性作爲hack的方式。


可是這樣會增加過多的文件加載,維護代碼數量也增加,有沒有更好的方式?


使用場景2


!--[if lt IE 7]>html class="ie6 oldie" lang="zh">![endif]-->


!--[if IE 7]>html class="ie7 oldie" lang="zh">![endif]-->


!--[if IE 8]>html class="ie8 oldie" lang="zh">![endif]-->


!--[if gt IE 8]>!--> html lang="zh"> !--![endif]-->複製代碼


場景1中的問題就解決了。通過選擇器的優先級就可以輕鬆解決差異。


有了條件註釋,JS也能從總獲益,免去的通過JS去判斷瀏覽器類型和版本了。


比如:如果你的頁面想使用html5標籤,條件註釋也能發揮作用。


!--[if lte IE 8]>


script>


(function(){


    var e="abbr,article,aside,audio,canvas,datalist,details,dialog,eventsource,figure,


footer,header,hgroup,mark,menu,meter,nav,output,progress,section,time,video".split(','),


        i=e.length; 


    while(i--){


      document.createElement(e[i]);


    }


 })();


 /script>


 ![endif]-->複製代碼


再比如:IE6的背景圖片緩存問題!--[if IE 6]>


script>


document.execCommand("BackgroundImageCache", false, true);


/script>


![endif]-->複製代碼


甚至還可以幫助JS直接獲取瀏覽器信息,大多數庫和方案識別瀏覽器都是通過userAgent串處理的,而且大部分的應用場景也是if (IExx) {doxx}


function isIE(v)


    var v=v || "",


    tester=document.createElement('div');


    tester.innerHTML='!--[if IE ' + v + ']>i>/i>![endif]-->';


    return !!tester.getElementsByTagName('i')[0];


}複製代碼


form: hect/


還可以在HTML代碼中玩


body>


!--[if lte IE 8]>


p>親愛的用戶,您的瀏覽器版本過低,建議您升級瀏覽器獲得更好的體驗..../p>


![endif]-->複製代碼


或許還有更多的玩法,就等待您的發現和分享了。


最後,條件註釋也不僅限於HTML中,JS也可以有,那就是JScript的特性了,這種坑爹的東東還是少用的好,因爲JS中的註釋總是要被壓縮掉的。


script>


/script>複製代碼


            


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章