代码之家  ›  专栏  ›  技术社区  ›  ishandutta2007

如何通过代码覆盖css变量?

  •  1
  • ishandutta2007  · 技术社区  · 2 年前

    如何从开发控制台覆盖css变量(或插入的扩展脚本)

    例如,对于google.com搜索页面,让我们说我想更改 rhs-width 变量

    我可以清楚地看到它是这样定义的:

    .srp {
        --rhs-width: 372px;
    }
    

    到目前为止我的尝试:

    var r = document.querySelector(':root');
    var rs = getComputedStyle(r);
    rs.getPropertyValue('--rhs-width')
    

    rs 别看我有它。

    我试过了 var r = document.querySelector('.srp'); 也一样,但结果是一样的。

    1 回复  |  直到 2 年前
        1
  •  1
  •   user23274861    2 年前

    获取元素,然后设置其样式的属性

    var r = document.querySelector('.srp');
    r.style.setProperty('--rhs-width', value);