【问题标题】:How to get the root style declarations on the HTML document? [duplicate]如何获取 HTML 文档的根样式声明? [复制]
【发布时间】:2020-09-16 00:56:17
【问题描述】:

我在:root 和全局命名空间中定义了一些样式。但是当我尝试以编程方式访问它们时,我收到了一个错误:

TypeError: Window.getComputedStyle: Argument 1 does not implement interface Element.

这是我的风格规则:

:root {
    --my-css-variable: green; 
}

这是我尝试过的:

var variable = "--my-css-variable";


// all of the following error
window.getComputedStyle(window).getPropertyValue(variable);
window.getComputedStyle(window.document).getPropertyValue(variable);
window.getComputedStyle(window.document.body).getPropertyValue(variable);

当我在页面上选择一个元素并查看调试工具时,它说元素是从 HTML 继承的:

【问题讨论】:

    标签: html css css-variables


    【解决方案1】:

    你可以试试:

    getComputedStyle(document.documentElement)
        .getPropertyValue('--my-css-variable');
    

    【讨论】:

      猜你喜欢
      • 2017-07-09
      • 2015-11-06
      • 1970-01-01
      • 2014-03-23
      • 1970-01-01
      • 2017-04-26
      • 1970-01-01
      • 2020-03-08
      • 1970-01-01
      相关资源
      最近更新 更多