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

查看:19
本文介绍了动态加载 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.

有什么帮助吗?谢谢

推荐答案

一旦 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)

请参阅有关 createStyleSheet 的 MSDN 文章了解更多详情.

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天全站免登陆