如何使用 JavaScript 获取 Less 中定义的 HTMLElement 属性?

问题描述 投票:0回答:1

当我尝试在 JavaScript 中使用 getCompulatedStyle 获取元素属性(例如“max-block-size”)时,我得到“none”。但是当我查看开发工具中的计算属性时,我可以看到它们具有实际值。我使用 Less 处理样式。当我将 Less 转换为 CSS 时,我的脚本工作正常。

const someElement = document.getElementById('element');
const styles = getComputedStyle(someElement);
const maxBlockSize = styles.getPropertyValue('max-block-size');
console.log(maxBlockSize);

PS。我使用 VSC + 实时预览 聚苯硫醚。 github

javascript less getcomputedstyle
1个回答
-1
投票

试试这个

const someElement = document.getElementById('element');
const styles = window.getComputedStyle(someElement);
const maxBlockSize = styles.getPropertyValue('max-block-size');
console.log(maxBlockSize);

© www.soinside.com 2019 - 2024. All rights reserved.