我想在点击特定div的按钮时显示背景颜色


<div class="mat-list-item"  *ngIf="authService.isAdmin()" (click)="openModal('site-settings', site.siteID)">
<span class="mat-list-item-content">{{ "Application.site_settings_label" | translate }}</span>
</div>

js

openModal() {

}

使用window.getComputedStyle((来获取背景颜色,否则不会检测到不在样式属性中或由JS设置的样式。

对于这个例子,我删除了函数的另一个参数。

function openModal() {
const div = document.getElementById('myId');
alert(window.getComputedStyle(div).backgroundColor);
}
#myId {
background-color: #ff0000;
}
<div class="mat-list-item" id="myId"  *ngIf="authService.isAdmin()" onClick="openModal()">
<span class="mat-list-item-content">{{ "Application.site_settings_label" | translate }}</span>
</div>