简体   繁体   English

动态加载 css 样式表在 IE 上不起作用

[英]Dynamically loading css stylesheet doesn't work on IE

I dynamically load a css stylesheet (with a little help from jQuery) like this:我像这样动态加载一个 css 样式表(在 jQuery 的帮助下):

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.这在 Firefox 和 Google Chrome 中工作正常,但在 IE 中无效

Any help?有什么帮助吗? Thanks谢谢

Once IE has processed all the styles loaded with the page, the only reliable way to add another stylesheet is with document.createStyleSheet(url)一旦 IE 处理了页面加载的所有样式,添加另一个样式表的唯一可靠方法是使用document.createStyleSheet(url)

See the MSDN article on createStyleSheet for a few more details.有关更多详细信息,请参阅有关 createStyleSheet 的MSDN 文章

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

You need to set the href attr last and only after the link elem is appended to the head:您需要最后设置 href attr 并且仅在链接 elem 附加到头部之后:

$('<link>')
  .appendTo('head')
  .attr({type : 'text/css', rel : 'stylesheet'})
  .attr('href', '/css/your_css_file.css');

Update更新

Nowadays the only purpose of IE and Edge is to download Chrome, so I recommend NOT bloating your code with custom support for IE or Edge and rather just ignoring their existence.现在 IE 和 Edge 的唯一目的是下载 Chrome,所以我建议不要用对 IE 或 Edge 的自定义支持来膨胀你的代码,而只是忽略它们的存在。

This also seems to work in IE:这似乎也适用于 IE:

var link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = '/includes/style.css';
document.getElementsByTagName('head')[0].appendChild(link);

This might also have something to do with it - Taken from Microsoft Support article :这可能也与它有关 - 取自Microsoft 支持文章

Styles on a webpage are missing or look incorrect when the page loads in the versions of Microsoft Internet Explorer ...在 Microsoft Internet Explorer 版本中加载页面时,网页上的样式丢失或看起来不正确...

...This problem occurs because the following conditions are true in Internet Explorer: ...发生此问题的原因是 Internet Explorer 中满足以下条件:

  • All style tags after the first 31 style tags are not applied.不应用前 31 个样式标签之后的所有样式标签。

  • All style rules after the first 4,095 rules are not applied.不应用前 4,095 条规则之后的所有样式规则。

  • On pages that uses the @import rule to continously import external style sheets that import other style sheets, style sheets that are more than three levels deep are ignored.在使用@import 规则连续导入导入其他样式表的外部样式表的页面上,深度超过三层的样式表将被忽略。

It seems that看起来

$('<link rel="stylesheet" type="text/css" href="' + url + '" />').appendTo('head'); 

works also in IE as long as the url is a fully qualified URI including the protocol...也可以在 IE 中使用,只要 url 是包含协议的完全限定 URI...

Open ie8 without the debugger open.在没有打开调试器的情况下打开 ie8。 When you get to after the point of dynamic stylesheet... open the debugger and voila, they should be there.当您到达动态样式表之后……打开调试器,瞧,它们应该在那里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM