IE8 样式不支持 setProperty 方法

IE8 Style Does not support setProperty method

提问人:Max Alexander Hanna 提问时间:9/26/2019 最后编辑:Max Alexander Hanna 更新时间:10/1/2019 访问量:137

问:

在以下代码行上出现此错误:

document.getElementsByTagName("table")[0].style.setProperty("background-color", "#D19000", "important");

enter image description hereObject doesn't support property or method 'setProperty'


只有在IE8中,去想想,有人知道如何修复它吗?

javascript 8 互联网浏览器 -7

评论

0赞 Nick Parsons 9/26/2019
也许试试... [0].style["backgroundColor"] = "#D19000"
1赞 4esn0k 9/13/2020
似乎可以按此处的原样使用 polyfillCSSStyleDeclaration.prototype.setProperty = function (name, value, priority) { this[name] = value; };CSSStyleDeclaration

答:

2赞 scunliffe 9/26/2019 #1

对于旧版本的 Internet Explorer,需要使用以下语法:

elem.style.backgroundColor = "#D19000";
elem.style.color = "red";

请注意,任何连字符的属性都需要采用 camelCase 格式。

评论

0赞 Max Alexander Hanna 9/26/2019
如何将 !important 属性添加到 backgroundColor ?
1赞 scunliffe 9/26/2019
如果我没记错的话,您应该能够将其设置为:。backgroundColor = "#D19000 !important"