首页 > 解决方案 > “getComputedStyle”无法识别元素

问题描述

在我的 React 应用程序中,我试图为我的组件使用getComputedStylegetPropertyValue,但它无法执行,因为它似乎无法识别我的元素。我明白了TypeError: Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element

const [dashboardToggle, setDashboardToggle] = useState(false);

  const boardElement = document.getElementsByClassName("dashboard");
  const boardPosition = window
    .getComputedStyle(boardElement[0])
    .getPropertyValue("position");
    

  console.log("this" + boardElement);
  console.log("this" + boardPosition);

return (<div className="App"><AnimatePresence>
        {dashboardToggle && (
          <Dashboard
            className="dashboard"
            dashboardToggle={dashboardToggle}
          ></Dashboard>
        )}
      </AnimatePresence>)
    </div>

当我控制台日志boardElement时,我得到[object HTMLCollection]. 我认为boardElement[0]应该是getComputedStyle.

这里似乎有什么问题?

标签: javascriptreactjsstylesgetcomputedstyle

解决方案


推荐阅读