[我想像在jquery中但在Angular 6中一样进行DOM操作

问题描述 投票:0回答:2

我想通过单击按钮来更改父div的背景颜色。

<div>
    <p>I want to change the background-color of the parent div by the click of the button.
    </p>
   <button (click)="changeColour()" id="buy-now">BUY NOW</button>
<div>
angular typescript angular6 dom-manipulation
2个回答
1
投票

您可以有条件地向父div添加一个类([class.red]="isRed",并在调用changeColor()函数(isRed = true)时更改条件。


0
投票

不要像JQuery那样考虑它!

但更详细的答案:

您的HTML将使用该类的表达式

<div [ngClass]="{'blue' : isBlue }"> 
    <p>I want to change the background-color of the parent div by the click of the button.
    </p>
   <button (click)="changeColour()" id="buy-now">BUY NOW</button>
<div>

然后您的TypeScript将切换值。您还可以根据需要使用表达式/类名:

export class AppComponent  {
isBlue = false;
  changeColour() {
    this.isBlue = !this.isBlue;
    console.log(this.isBlue);
  }
  name = 'Angular';
}

See working example:

© www.soinside.com 2019 - 2024. All rights reserved.