gpt4 book ai didi

javascript - 具有状态 (.active) 的元素的 getAttribute

转载 作者:太空狗 更新时间:2023-10-29 18:17:45 26 4
gpt4 key购买 nike

我无法检查元素属性的事件状态。我在下面尝试过,但它返回 false,即使该元素具有处于事件状态的属性 - (.c-banner.active 存在)

ngAfterViewInit() {
const bannerElm = document.getElementById("banner");
const isActive = bannerElm && bannerElm.getAttribute("class")
.indexOf("c-banner.active") !== -1; // returned false
}

最佳答案

为什么不使用 classListcontains

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com