javascript - 无法使用 getElementById() 获取应用了“显示:无”属性的元素
问题描述
我遇到了一个问题,即该getElementById()
函数无法在页面上获取display: none
应用了该属性的特定元素,即使它在 DOM 中可见(我可以看到 div 及其 id 存在于最终呈现的页)。
有没有解决的办法?
这是代码:
togglePanel() {
const panelId = this.accordionItem.querySelector("#collapsible-panel");
this.shouldShowAccordion = !this.shouldShowAccordion;
if (this.shouldShowAccordion) {
panelId.classList.remove("collapsed");
}
else {
panelId.classList.add("collapsed");
}
}
“collapsible-panel”是display: none
应用到它的 div 的 ID。
解决方案
setTimeout(() => {
document.querySelector("div[id='collapsiblepanel']").style.display = 'block';
}, 2000)
#collapsiblepanel {
width: 200px;
height: 100px;
background-color: red;
display: none;
}
Following is ana example where I am selecting a div which has the value of display as none. After 2 seconds I am setting up it's display to block.
<div id="collapsiblepanel">
</div>
推荐阅读
- kotlin - 在函数中引用扩展此类的类
- swiftui - SwiftUI Circle View 动画故障
- java - JPanel 不再获得新值
- coldfusion - 变量内部的冷融合变量
- javascript - 使用 javascript style.height 转换高度
- c# - .net C# 将值作为查询字符串发布到带有按钮的另一个页面
- kdb - 如何使用 BFGS 求解 KDB 中的线性模型?
- android - android上的离子后退按钮结束应用程序
- android - RxJava2 根据 if/else 逻辑做出可重复的动作并返回结果
- r - 如何在 wordcloud2 中使用“min.freq”?