javascript - “getComputedStyle”无法识别元素
问题描述
在我的 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
.
这里似乎有什么问题?
解决方案
推荐阅读
- ruby-on-rails - 在命名空间 api (Api::V1) 中,我无法访问序列化程序
- objective-c - 以编程方式识别带有 Notch 的 iPhone
- r - 如何使用 ggplot 在同一图表上绘制具有两种不同配色方案的两组数据?
- javascript - 为什么 Chrome(在 Electron 内部)会突然重定向到 chrome-error://chromewebdata?
- php - Wordpress 导航子菜单间距
- bash - 尾随多个文件并从文件中输入搜索词,输出使用文件名搜索的项目
- python - 如何将整数放在列表的末尾,使其不会成为 Python 中的“属性错误”?
- mlflow - 将运行迁移到 MLFlow 0.9
- php - Symfony 寄存器未找到 CSRF 令牌无效
- c# - 删除 EF Core 迁移会删除与迁移无关的列