Код: Выделить всё
getComputedStyle()Я прочитал документы и попробовал это предложения. Я хочу исправить приведенный выше фрагмент для хрома.
Код: Выделить всё
let cs = window.getComputedStyle(document.documentElement);
// works
console.log('cs', cs.getPropertyValue('--test'));
// doesn't work in Chrome
for (let i of cs) {
if (i.indexOf('--') == 0) {
console.log('for let', i);
}
}< /code>
:root {
--test: #000;
}Update: get_styles() was created that returns browser-independent style map
Код: Выделить всё
function get_styles() {
if ('computedStyleMap' in document.documentElement) {
// WebKit
return document.documentElement.computedStyleMap();
} else {
// Gecko
let cs = window.getComputedStyle(document.documentElement);
return [...cs].map((key) => [key, cs.getPropertyValue(key)]);
}
}
let styles = get_styles();
for (let [key, val] of styles) {
if (key.indexOf('--') == 0) {
console.log(key + ' ' + val);
}
}< /code>
:root {
--test: #000;
}Подробнее здесь: https://stackoverflow.com/questions/794 ... -in-chrome
Мобильная версия