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);

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

Any help? Thanks

推荐答案

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

有关更多详细信息,请参阅createStyleSheet上的MSDN article.

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

Jquery相关问答推荐

bootstrap - Select Select 器不能在下拉菜单中 Select

加入了两个 select 语句,但数据显示了两次而不是一次

为什么我不能在 window.onload 事件的处理程序中将 $ jQuery 对象作为参数传递?

可以推迟 jQuery 的加载吗?

jQuery 上传进度和 AJAX 文件上传

$(document).ready(function() VS $(function(){

使用 JSONP 时如何捕获 jQuery $.getJSON(或数据类型设置为jsonp的 $.ajax)错误?

使用 jQuery 和 HTML 导出为 CSV

使用 JQuery 清除下拉列表

如何在 Jquery 中通过索引获取子元素?

如何立即启动 setInterval 循环?

jQuery切换文本?

使用 jQuery 刷新(重新加载)页面一次?

如何在实际图像下载时显示加载图像

如何签入用户已在 Google recaptcha 中选中复选框的 js?

如何使用 jQuery 格式化电话号码

jquery - 从一个非常大的表中删除所有行的最快方法

如何在jQuery中 Select 最后一个子元素?

如何使用 bootstrap 中的 selectpicker 插件在 Select 时设置选定值

JQuery 判断 DOM 中的重复 id