0

我的代码中有一个问题,负责交换明暗等主题。

放入一段特定的代码。给出一个错误,消息是这样的

Uncaught TypeError: Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element'.
at getStyle (dark.js:6)
at dark.js:9

我的代码是:

const dark = document.querySelector("root");
const checkbox = document.querySelector("input[name=theme]");

const getStyle = (element, style) =>
  window.getComputedStyle(element).getPropertyValue(style);

const initialColors = {
  header: getStyle(dark, "--card-income"),
};

const darkMode = {
  header: "#13224F",
};

const transformKey = (key) =>
  "--" + key.replace(/([A-Z])/, "-$1").toLowerCase();

const changeColors = (colors) => {
  Object.keys(colors).map((key) =>
    dark.style.setProperty(transformKey(key), colors[key])
  );
};

checkbox.addEventListener("change", ({ target }) => {
  target.checked ? changeColors(darkMode) : changeColors(initialColors);
  console.log(target.checked);
});
4

0 回答 0