设置自定义样式属性在 IE11 中不起作用



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

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

这在火狐谷歌浏览器中工作正常,但在IE 11中也尝试过

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

他们都没有工作.

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

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

回答:IE 中 CSS 变量的解决方法?

这对我来说是最好的解决方案,无需安装:

https://www.npmjs.com/package/ie11-custom-properties

将此复制到 html 的头部:

<script>window.MSInputMethodContext && document.documentMode && document.write('<script src="https://cdn.jsdelivr.net/gh/nuxodin/ie11CustomProperties@4.1.0/ie11CustomProperties.min.js"><x2fscript>');</script> 

最新更新