繁体   English   中英

在Angular 2中更改ViewChild上的元素类

[英]Change element class on ViewChild in Angular 2

我在Angular 2中使用ViewChild来隔离一个元素。 我可以直接操作样式,但是如果可以更改元素样式类,我找不到文档。

这是示例代码:

    export class HomeComponent implements OnInit {

    @ViewChild('slideBg') el:ElementRef;

    ngAfterViewInit {
        // style can be changed
        this.el.nativeElement.style.background = 'red';
        // does not work:
        this.el.nativeElement.class = 'myCSSclass';
    }

    …

}

想看看这是否可行以及如何实现。 任何帮助赞赏。

好像你在寻找className属性:

this.el.nativeElement.className = 'myCSSclass';

Element.className

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM