I amQiana , WELCOME.

NO TRY , NO HIGH

HTML 中判断IE浏览器

项目中老是要修IE的layout,IE这个让人想要放弃又欲罢不能的家伙。。。
想要查询CSS或者某些库在IE或者其他浏览器的支持情况,可以使用canIUse这个网站,很赞!
比如说我想搜索translate这个属性的支持情况,结果一目了然。
canIUseTranslate


言归正传

简单记录下在HTMl中如何区分IE以及其他浏览器:

  • 所有非IE浏览器都去加载not-ie.css
    <!--[if !IE]><!-->
        <link rel="stylesheet" type="text/css" href="not-ie.css" />
     <!--<![endif]-->
    
  • 所有版本IE都加载all-ie-only.css
    <!--[if IE]>
        <link rel="stylesheet" type="text/css" href="all-ie-only.css" />
    <![endif]-->
    
  • 特定版本的IE(比如7)去加载ie7.css
    <!--[if IE 7]>
        <link rel="stylesheet" type="text/css" href="ie7.css">
    <![endif]-->
    
  • IE7(可改为其他版本)以下去加载ie6-and-down.css

    <!--[if lt IE 7]>
        <link rel="stylesheet" type="text/css" href="ie6-and-down.css" />
    <![endif]-->
    
    or
    
    <!--[if lte IE 6]>
        <link rel="stylesheet" type="text/css" href="ie6-and-down.css" />
    <![endif]-->
    
  • IE7(可为其他版本)及以上版本去加载ie7-and-up.css

    <!--[if gt IE 6]>
        <link rel="stylesheet" type="text/css" href="ie7-and-up.css" />
    <![endif]-->
    
    or
    
    <!--[if gte IE 7]>
        <link rel="stylesheet" type="text/css" href="ie7-and-up.css" />
    <![endif]-->
    

在javaScript中通过UA判断,原理是IE浏览器的UA里有一个Trident字符,是IE专有属性。我们可以通过正则匹配UA里的Trident字符串。(有就是IE,没有就不是)

function isIE(){
    truevar ua = navigator.userAgent;
    truereturn ua.search(/Trident/ie);
 }