Geo*_*rge 5 dom typescript visual-studio-code ionic3 angular
我正在使用 ionic 3 构建混合移动应用程序,要求之一是用户能够动态更改工具栏颜色。页面渲染后,html 如下所示:
<div id="divICanControl"> //this div i can control
<div> //but this one is generated by the framework and this is the div that change the background color of the toolbar
</div>
</div>
我尝试执行以下操作:
document.getElementById('divICanControl').childNodes[0].style.backgroundColor = this.someColor;
它偶尔可以工作,但它在 vscode 中创建了以下错误:
[ts] Property 'style' does not exist on type 'Node'.
它停止了应用程序的构建。
我现在正在寻找使用角度操纵 dom 的正确方法。
感谢您的帮助,请记住我是 Angular 5 和 TypeScript 的新手。
推荐的元素样式设置方法不是使用 document....style。它带有渲染器。观察:
<div id="first">
first
<div id="second">
second
</div>
</div>
<button (click)="change()">Change</button>
import { Component, OnInit, Renderer2 } from '@angular/core';
constructor(private renderer: Renderer2) { }
change() {
const parent: HTMLElement = document.getElementById('first');
const child = parent.children[0];
this.renderer.setStyle(child, 'background-color', 'red');
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16994 次 |
| 最近记录: |