今天使用了动态加载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)
修改代码如下,就可以成功加载啦!
$(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);
}