H5的兼容性处理问题(部分方法)

IE8及以下浏览器无法识别新标签和新属性,导致页面展示不对,解决方法如下:

一、非媒体标签(如<video><audio>等):

通过<script>标签引入官方出的JS文件即可(html5shiv.js文件代码附最后)

代码:

<script src="./html5shiv.js"></script>

为了避免非IE8 以下都加载此资源,浪费请求,可以写只有ie8 以下才认识的注释:

<head>
  <meta charset="UTF8">
  <title>document</title>
  <!--[if lt ie 9]>
  <script src="./html5shiv.js"></script>
  <![endif]-->
</head>

注释解释:
如果 小于< ie9 ,则执行JS标签引入html5shiv.js 文件,判断结束
这样的话,只有ie8以下才能在注释内识别到,再加载这个兼容性文件了

html5shiv.js 文件在下方

/*! HTML5 Shiv v3.6.1 | @afarkas @jdalton @jon_neal @rem | MIT/GPL2 Licensed */
(function (window, document) {
  /*jshint evil:true */
  /** Preset options */
  var options = window.html5 || {}



  /** Used to skip problem elements */
  var reSkip = /^<|^(?:button|map|select|textarea|object|iframe|option|optgroup)$/i



  /** Not all elements can be cloned in IE **/
  var saveClones = /^(?:a|b|code|div|fieldset|h1|h2|h3|h4|h5|h6|i|label|li|ol|p|q|span|strong|style|table|tbody|td|th|tr|ul)$/i



  /** Detect whether the browser supports default html5 styles */
  var supportsHtml5Styles



  /** Name of the expando, to work with multiple documents or to re-shiv one document */
  var expando = '_html5shiv'



  /** The id for the the documents expando */
  var expanID = 0



  /** Cached data for each document */
  var expandoData = {}



  /** Detect whether the browser supports unknown elements */
  var supportsUnknownElements;



  (function () {
    try {
      var a = document.createElement('a')
      a.innerHTML = '<xyz></xyz>'
      //if the hidden property is implemented we can assume, that the browser supports basic HTML5 Styles
      supportsHtml5Styles = ('hidden' in a)



      supportsUnknownElements = a.childNodes.length == 1 || (function () {
        // assign a false positive if unable to shiv
        (document.createElement)('a')
        var frag = document.createDocumentFragment()
        return (
          typeof frag.cloneNode == 'undefined' ||
          typeof frag.createDocumentFragment == 'undefined' ||
          typeof frag.createElement == 'undefined'
        )
      }())
    } catch (e) {
      supportsHtml5Styles = true
      supportsUnknownElements = true
    }



  }())



  /*--------------------------------------------------------------------------*/



  /**
   * Creates a style sheet with the given CSS text and adds it to the document.
   * @private
   * @param {Document} ownerDocument The document.
   * @param {String} cssText The CSS text.
   * @returns {StyleSheet} The style element.
   */
  function addStyleSheet (ownerDocument, cssText) {
    var p = ownerDocument.createElement('p'),
      parent = ownerDocument.getElementsByTagName('head')[0] || ownerDocument.documentElement



    p.innerHTML = 'x<style>' + cssText + '</style>'
    return parent.insertBefore(p.lastChild, parent.firstChild)
  }



  /**
   * Returns the value of `html5.elements` as an array.
   * @private
   * @returns {Array} An array of shived element node names.
   */
  function getElements () {
    var elements = html5.elements
    return typeof elements == 'string' ? elements.split(' ') : elements
  }


  /**
 * Returns the data associated to the given document
 * @private
 * @param {Document} ownerDocument The document.
 * @returns {Object} An object of data.
 */
  function getExpandoData (ownerDocument) {
    var data = expandoData[ownerDocument[expando]]
    if (!data) {
      data = {}
      expanID++
      ownerDocument[expando] = expanID
      expandoData[expanID] = data
    }
    return data
  }



  /**
   * returns a shived element for the given nodeName and document
   * @memberOf html5
   * @param {String} nodeName name of the element
   * @param {Document} ownerDocument The context document.
   * @returns {Object} The shived element.
   */
  function createElement (nodeName, ownerDocument, data) {
    if (!ownerDocument) {
      ownerDocument = document
    }
    if (supportsUnknownElements) {
      return ownerDocument.createElement(nodeName)
    }
    if (!data) {
      data = getExpandoData(ownerDocument)
    }
    var node



    if (data.cache[nodeName]) {
      node = data.cache[nodeName].cloneNode()
    } else if (saveClones.test(nodeName)) {
      node = (data.cache[nodeName] = data.createElem(nodeName)).cloneNode()
    } else {
      node = data.createElem(nodeName)
    }



    // Avoid adding some elements to fragments in IE < 9 because
    // * Attributes like `name` or `type` cannot be set/changed once an element
    //   is inserted into a document/fragment
    // * Link elements with `src` attributes that are inaccessible, as with
    //   a 403 response, will cause the tab/window to crash
    // * Script elements appended to fragments will execute when their `src`
    //   or `text` property is set
    return node.canHaveChildren && !reSkip.test(nodeName) ? data.frag.appendChild(node) : node
  }



  /**
   * returns a shived DocumentFragment for the given document
   * @memberOf html5
   * @param {Document} ownerDocument The context document.
   * @returns {Object} The shived DocumentFragment.
   */
  function createDocumentFragment (ownerDocument, data) {
    if (!ownerDocument) {
      ownerDocument = document
    }
    if (supportsUnknownElements) {
      return ownerDocument.createDocumentFragment()
    }
    data = data || getExpandoData(ownerDocument)
    var clone = data.frag.cloneNode(),
      i = 0,
      elems = getElements(),
      l = elems.length
    for (; i < l; i++) {
      clone.createElement(elems[i])
    }
    return clone
  }



  /**
   * Shivs the `createElement` and `createDocumentFragment` methods of the document.
   * @private
   * @param {Document|DocumentFragment} ownerDocument The document.
   * @param {Object} data of the document.
   */
  function shivMethods (ownerDocument, data) {
    if (!data.cache) {
      data.cache = {}
      data.createElem = ownerDocument.createElement
      data.createFrag = ownerDocument.createDocumentFragment
      data.frag = data.createFrag()
    }





    ownerDocument.createElement = function (nodeName) {
      //abort shiv
      if (!html5.shivMethods) {
        return data.createElem(nodeName)
      }
      return createElement(nodeName, ownerDocument, data)
    }



    ownerDocument.createDocumentFragment = Function('h,f', 'return function(){' +
      'var n=f.cloneNode(),c=n.createElement;' +
      'h.shivMethods&&(' +
      // unroll the `createElement` calls
      getElements().join().replace(/\w+/g, function (nodeName) {
        data.createElem(nodeName)
        data.frag.createElement(nodeName)
        return 'c("' + nodeName + '")'
      }) +
      ');return n}'
    )(html5, data.frag)
  }



  /*--------------------------------------------------------------------------*/



  /**
   * Shivs the given document.
   * @memberOf html5
   * @param {Document} ownerDocument The document to shiv.
   * @returns {Document} The shived document.
   */
  function shivDocument (ownerDocument) {
    if (!ownerDocument) {
      ownerDocument = document
    }
    var data = getExpandoData(ownerDocument)



    if (html5.shivCSS && !supportsHtml5Styles && !data.hasCSS) {
      data.hasCSS = !!addStyleSheet(ownerDocument,
        // corrects block display not defined in IE6/7/8/9
        'article,aside,figcaption,figure,footer,header,hgroup,nav,section{display:block}' +
        // adds styling not present in IE6/7/8/9
        'mark{background:#FF0;color:#000}'
      )
    }
    if (!supportsUnknownElements) {
      shivMethods(ownerDocument, data)
    }
    return ownerDocument
  }



  /*--------------------------------------------------------------------------*/



  /**
   * The `html5` object is exposed so that more elements can be shived and
   * existing shiving can be detected on iframes.
   * @type Object
   * @example
   *
   * // options can be changed before the script is included
   * html5 = { 'elements': 'mark section', 'shivCSS': false, 'shivMethods': false };
   */
  var html5 = {



    /**
     * An array or space separated string of node names of the elements to shiv.
     * @memberOf html5
     * @type Array|String
     */
    'elements': options.elements || 'abbr article aside audio bdi canvas data datalist details figcaption figure footer header hgroup mark meter nav output progress section summary time video',



    /**
     * A flag to indicate that the HTML5 style sheet should be inserted.
     * @memberOf html5
     * @type Boolean
     */
    'shivCSS': (options.shivCSS !== false),



    /**
     * Is equal to true if a browser supports creating unknown/HTML5 elements
     * @memberOf html5
     * @type boolean
     */
    'supportsUnknownElements': supportsUnknownElements,



    /**
     * A flag to indicate that the document's `createElement` and `createDocumentFragment`
     * methods should be overwritten.
     * @memberOf html5
     * @type Boolean
     */
    'shivMethods': (options.shivMethods !== false),



    /**
     * A string to describe the type of `html5` object ("default" or "default print").
     * @memberOf html5
     * @type String
     */
    'type': 'default',



    // shivs the document according to the specified `html5` object options
    'shivDocument': shivDocument,



    //creates a shived element
    createElement: createElement,



    //creates a shived documentFragment
    createDocumentFragment: createDocumentFragment
  }



  /*--------------------------------------------------------------------------*/



  // expose html5
  window.html5 = html5



  // shiv the document
  shivDocument(document)



}(this, document))

二、添加元信息,让浏览器处于最优渲染模式

<!-- 设置IE总是使用最新的文档模式进行渲染 -->
<mata http_equiv="X-UA-Compatible" content="IE=Edge">

<!-- 优先使用webkit(Chrome)内核进行渲染,针对360等壳内核 -->
<meta name="renderer" content="webkit">

第一行是写给ie 的,第二行是写给国内双核浏览器看的。国内双核浏览器大部分主流是Chromewebkit ,和IEtrident

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 220,884评论 6 513
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,212评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 167,351评论 0 360
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,412评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,438评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,127评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,714评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,636评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,173评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,264评论 3 339
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,402评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,073评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,763评论 3 332
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,253评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,382评论 1 271
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,749评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,403评论 2 358

推荐阅读更多精彩内容