我想像在jquery中一样进行DOM操作,但是在Angular 6中
我想通过单击按钮来更改父div的背景色我想像在jquery中一样进行DOM操作,但是在Angular 6中,angular,typescript,angular6,dom-manipulation,Angular,Typescript,Angular6,Dom Manipulation,我想通过单击按钮来更改父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> 我想通过单击按钮来更改父di
<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>
我想通过单击按钮来更改父div的背景色。
立即购买
您可以有条件地将类添加到父div([class.red]=“isRed”
),并在调用changeColor()
函数(isRed=true
)时更改条件。不要把它看作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>
不是这样,您正在更改isBlue的值,假设它存在于多个div中,然后呢?@FurquanSaifi然后您可以使用数组,或数组上的表达式,或自定义模块/指令,以便每个div都是自己的对象,具有您关心的属性。这是您应该采用的方法,直接DOM操作是不好的。而且,您的代码甚至没有建议您使用的模型,或者许多潜在的div。
export class AppComponent {
isBlue = false;
changeColour() {
this.isBlue = !this.isBlue;
console.log(this.isBlue);
}
name = 'Angular';
}