试图让我的网站在 IE6 中运行,但在处理一些我需要做的运行时 css 问题时遇到了问题。
这两种我都试过了:
$.rule('#post'+ i + '{ visibility:hidden;}').appendTo('style');
-
var postStyle = document.createElement('style');
postStyle.type = 'text/css';
postStyle.innerHTML = '#post'+ i + '{ visibility:hidden;}';
document.getElementsByTagName('head'[0].appendChild(postStyle);
适用于其他浏览器,但不适用于 IE。我收到此错误:
“对象不支持这个属性或方法”
有什么想法吗?
最佳答案
在 IE 中,你必须这样做:
postStyle.styleSheet.cssText = whatever;
我通常使用 try
/catch
设置来完成此操作。
$('head').append($('<style/>', { id: "replaced-colors" }));
try {
$('#replaced-colors').html(replaced);
}
catch (_ie) {
$('#replaced-colors')[0].styleSheet.cssText = replaced;
}
作为现实生活中的例子。
关于javascript - 在 IE6 运行时添加 CSS 规则 - 对象不支持此属性或方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10980757/