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

标签 javascript angular typescript

我无法检查元素属性的事件状态。我在下面尝试过,但它返回 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/

相关文章:

javascript - Kendo UI 模板刷新

javascript - Angular2 : template work, 但 templateUrl 不是

Angular 6 : pass viewContainerRef from template

angular - 如何使用 angualr2 和 typescript 将图像上传到 azure 存储 blob

typescript - 为什么 bool 值变为真?

javascript - 获取已定义坐标的图层和要素

javascript - jQuery 未在 HTML 上加载

javascript - HTML 5 文件系统访问类型错误

javascript - 还有什么是规范的 javascript,console.log

typescript - 将 MomentJS 与 TypeScript 结合使用 - moment() 有什么类型?