我正在尝试在我的 typescript 中使用 javascript 设置元素的样式,但它不起作用。这是我尝试做的:
const element = document.getElementsByClassName('current');
element.style.backgroundColor = "" + this.styles.style.mainIdentifyingColor;
但我得到错误:
Property 'style' does not exist on HTMLCollectionOf.
我也尝试过 setAttribute 但同样的事情..
最佳答案
我知道我没有完全回答你的问题,但你尝试过使用 NgStyle 吗? docs
HTML
<div [ngStyle]="{ 'background-color': myCustomColor }">
</div>
TS
someFunctionToBeCalled() {
this.myCustomColor=this.styles.style.mainIdentifyingColor;
}
关于javascript - element.style 在 typescript 中不起作用 [Angular 5],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49634711/