IE動態載入CSS link失敗

來源:互聯網
上載者:User

今天使用了動態載入CSS的方法,但是如下動態調用的話是無法正確在IE中載入,當然在firefox和chrome中是正常的。

$(function(){      var linkTmp = $('<link rel="stylesheet" type="text/css" />');      linkTmp.attr({          'href':'source/uploadify/resource/uploadify.css'      });      $('head').eq(0).append(linkTmp);})

當時我特意去查看了一下html發現在head中是有成功的插入該語句
<link rel="stylesheet" type="text/css" href="source/uploadify/resource/uploadify.css"/>
但是為什麼IE不會去動態載入呢?
看到一句比較經典的解釋:
Once IE has processed all the styles loaded with the page, the only reliable way to add another stylesheet is with document.createStyleSheet(url)
修改代碼如下,就可以成功載入啦!

url ='style.css';if(document.createStyleSheet){    document.createStyleSheet(url);}else{      var linkTmp = $('<link rel="stylesheet" type="text/css" />');      linkTmp.attr({          'href':'source/uploadify/resource/uploadify.css'      });      $('head').eq(0).append(linkTmp);}
相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.