Trouble programmatically adding CSS to IE

房东的猫 提交于 2019-12-04 10:13:27

I just tried this

javascript:(function(d) { d.createStyleSheet().cssText="* {color:blue !important;}"   })(document);

and

javascript:(function(d) { d.createStyleSheet("http://myschemas.com/pub/clear.css") })(document);

from IE on amazon.com and both worked. Maybe you need to add the !important to some items of your css to be sure they take effect now?

UPDATE: Found a possible solution for you...

javascript:(function(c) {c[c.length-1].addImport("http://myschemas.com/pub/clear.css")})(document.styleSheets);

Hope it helps you.

Looking for an answer, I have found that the 31 stylesheets limit raise this exception when loading CSS programatically:

http://www.telerik.com/community/forums/aspnet-ajax/general-discussions/not-enough-storage-is-available-to-complete-this-operation.aspx

The original limitation is described in a Knowledge Base document (suppossed only to happen on IE8 and below, but repeatedly reported as happening in IE9):

http://support.microsoft.com/kb/262161

This is all I do, and it I have never seen it not work.

loadCss = function( name, url ) {

    var head = document.getElementsByTagName('head')[0];

    var link = document.createElement("link");

    link.setAttribute("type", "text/css");

    link.setAttribute("rel", "stylesheet");

    link.setAttribute("href", url);

    link.setAttribute("media", "screen");

    head.appendChild(link);
};

I was doing something similar using jQuery and found that I had to do it in this order:

var $link = $('<link>');
$('head').add($link);
$link.attr({
  type: 'text/css',
  // ... etc ...
  media: 'screen'
});

Otherwise it doesn't work in IE (IE7, haven't looked at IE8 yet).

HTH

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!