在我的 React 应用程序中,我试图为我的组件使用getComputedStyle
和getPropertyValue
,但它无法执行,因为它似乎无法识别我的元素。我明白了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
.
这里似乎有什么问题?