angular - 单击一个按钮会触发其他具有相同事件功能的按钮
问题描述
我正在处理一个离子项目,并且在我的项目中,当我单击 1 个按钮时,其他按钮也会更改它们的值,因为它们具有相同的事件。但我想要相反,点击一个按钮应该只改变一个特定按钮的值。
HTML<table class="student_list">
<tr><ion-item *ngFor="let record of data">
<ion-avatar slot="start">
<img src="assets/student.png">
</ion-avatar>
<ion-label class="text2">{{record.name}} {{record.id}}
<span class="button1"><button (click)="toggleBackgroundColor()" ion-button [style.background-color]="hexColor" class="btn">{{text}}</button></span>
</ion-label>
</ion-item></tr>
</table>
TS文件
public toggleBackgroundColor(): void {
if(this.hexColor === '#1e90ff') {
this.hexColor = '#ff0000'
this.text='A';
} else {
this.hexColor = '#1e90ff';
this.text = 'P'
} }
解决方案
您应该在调用调用的特定标签的函数调用上放置一个标识符。
<button id="changeable" (click)="toggleBackgroundColor('changeable')" ion-button [style.background-color]="hexColor" class="btn">{{text}}</button>
和功能:
public toggleBackgroundColor(id): void {
var element = document.getElementById(id);
if(element.hexColor === '#1e90ff') {
element.hexColor = '#ff0000'
element.text='A';
} else {
element.hexColor = '#1e90ff';
element.text = 'P'
} }
推荐阅读
- python - 对元组使用startswith,在Python中返回匹配的字符串
- python - 如何在 Pandas 数据框中查找 re.search 失败的行
- php - 在 2 行上制作 prestashop 1.7 名称和描述
- typescript - 将 Typescript Record 或类字典类型与固定键类型合并?
- c# - 使用 ADO.NET 的批处理操作
- c - C中动态二维数组的限制
- jquery - 更改事件的 JQuery 在弹出窗口中不起作用
- boost - How to build boost libraries using cmake
- java - 如果spring jpa中不存在列,如何将实体属性默认值标记为null
- python-3.x - 替换数据框中单元格内的多个值 - Python/Pandas