我无法检查元素属性的事件状态。我在下面尝试过,但它返回 false,即使该元素具有处于事件状态的属性 - (.c-banner.active
存在)
ngAfterViewInit() {
const bannerElm = document.getElementById("banner");
const isActive = bannerElm && bannerElm.getAttribute("class")
.indexOf("c-banner.active") !== -1; // returned false
}
最佳答案
为什么不使用 classList 和 contains?
classList
.contains();
ngAfterViewInit() {
const bannerElm = document.getElementById("banner");
const isActive = bannerElm && bannerElm.classList.contains('c-banner') && bannerElm.classList.contains('active');
}
关于javascript - 具有状态 (.active) 的元素的 getAttribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55010297/