动态加载CSS样式表在IE上不工作 [英] Dynamically loading css stylesheet doesn't work on IE

查看:124
本文介绍了动态加载CSS样式表在IE上不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我动态加载css样式表(从jQuery的一点帮助)如下:

I dynamically load a css stylesheet (with a little help from jQuery) like this:

var head = document.getElementsByTagName('head')[0];
$(document.createElement('link'))
    .attr({ type: 'text/css', href: '../../mz/mz.css', rel: 'stylesheet' })
    .appendTo(head);

这在Firefox和Google Chrome中可以正常工作,但在IE中不是

This works fine in Firefox and Google Chrome, but not in IE.

有任何帮助吗?
感谢

Any help? Thanks

推荐答案

一旦IE处理了加载了页面的所有样式,唯一可靠的方式添加另一个样式表是 document.createStyleSheet(url)

Once IE has processed all the styles loaded with the page, the only reliable way to add another stylesheet is with document.createStyleSheet(url)

请参阅 MSDN文章关于createStyleSheet的更多详细信息。

See the MSDN article on createStyleSheet for a few more details.

url = 'style.css';
if (document.createStyleSheet)
{
    document.createStyleSheet(url);
}
else
{
    $('<link rel="stylesheet" type="text/css" href="' + url + '" />').appendTo('head'); 
}

这篇关于动态加载CSS样式表在IE上不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆