首页 > 解决方案 > 设置自定义样式属性在 IE11 中不起作用

问题描述

我有这个简单的 Javascript 代码来更改自定义 CSS 属性

var root = document.documentElement;
root.style.setProperty("--custom-bg", "#4f5357");

这在 Firefox Google Chrome 中运行良好,但在 IE 11 中不起作用,也尝试过

root['--custom-bg'] = "#4f5357";
root.attr("--custom-bg", "#4f5357");
root.style['--custom-bg'] = "#4f5357";

他们都没有工作。

标签: javascripthtmljquerycssstyles

解决方案


IE11支持 css 自定义属性,因此不支持这种setProperty方法。

查看这个css-vars-ponyfill,它旨在添加基本支持。


推荐阅读