如何在IE8中动态创建CSSclass
How to dynamically create CSS class in IE8
我需要在 IE8 中使用 JavaScript 动态创建 CSS 样式表 class。
我在其他浏览器上使用了以下代码:
var style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = '.cssClass { color: #F00; }';
document.getElementsByTagName('head')[0].appendChild(style);
它在除 IE8 之外的所有浏览器中工作正常。如何在IE8中实现同样的功能?
根据MSDN:
The innerHTML property is read-only on the col, colGroup, frameSet, html, head, style, table, tBody, tFoot, tHead, title, and tr objects.
所以,尝试用innerText
来写这些class。
已更新:
您可以使用:
style.styleSheet.cssText = '.cssClass { color: #F00; }';
或者做个测试:
if (style.styleSheet){
style.styleSheet.cssText = '.cssClass { color: #F00; }';
} else {
style.appendChild(document.createTextNode('.cssClass { color: #F00; }'));
}
希望,它现在可以运行了! :)
我需要在 IE8 中使用 JavaScript 动态创建 CSS 样式表 class。
我在其他浏览器上使用了以下代码:
var style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = '.cssClass { color: #F00; }';
document.getElementsByTagName('head')[0].appendChild(style);
它在除 IE8 之外的所有浏览器中工作正常。如何在IE8中实现同样的功能?
根据MSDN:
The innerHTML property is read-only on the col, colGroup, frameSet, html, head, style, table, tBody, tFoot, tHead, title, and tr objects.
所以,尝试用innerText
来写这些class。
已更新:
您可以使用:
style.styleSheet.cssText = '.cssClass { color: #F00; }';
或者做个测试:
if (style.styleSheet){
style.styleSheet.cssText = '.cssClass { color: #F00; }';
} else {
style.appendChild(document.createTextNode('.cssClass { color: #F00; }'));
}
希望,它现在可以运行了! :)