簡體   English   中英

基於 innerHTML 值的顏色

[英]Color based on value of innerHTML

我是 web 開發人員的新手,需要幫助。 如何根據值更改顏色?

<mat-accordion fxLayout="column" fxLayoutAlign="space-around center" fxLayoutGap="20px" >
      <mat-expansion-panel class="mat-elevation-z3" *ngFor="let resultSkill of ReportData; let l=index" >
        <mat-expansion-panel-header class="description"  [attr.data-target]="'#happy'+l" >
          <mat-panel-title>
            {{resultSkill.ip_title}}
          </mat-panel-title>
          <span class="button-align2" [innerHTML]="resultSkill.range1"></span>
        </mat-expansion-panel-header>
        <mat-panel-description id="happy{{l}}" class="collapse">
        </mat-panel-description>
        <p class="collapse-message"  [innerHTML]="resultSkill.text" style="font-size: 14px;"></p>
        <p class="collapse-message" [innerHTML]="resultSkill.result" style="font-size: 14px;"></p>
        <p class="collapse-message" style="text-align: center; color: #006fd2"><b>How to overcome this?</b></p>
        <p class="collapse-message" [innerHTML]="resultSkill.tips" style="font-size: 14px;"></p>
        <hr class="style5">
      </mat-expansion-panel>
      <br/>
    </mat-accordion>

CSS:
.button-align2{
    float: right;
    margin-right: 0;
    font-size: 16px;
  }

外觀快照

你需要一個簡單的 JS 腳本。 查看下面的代碼片段或https://codepen.io/ma-henderson/pen/OJRGEqq

 const low_color = 'red'; const mid_color = 'yellow'; const high_color = 'green'; const buttons = document.querySelectorAll(".button2") for (let i = 0; i < buttons.length; i++) { if (buttons[i].dataset.skill == 'range1') { buttons[i].style.backgroundColor = high_color; } else if (buttons[i].dataset.skill == 'range2') { buttons[i].style.backgroundColor = mid_color; } else { buttons[i].style.backgroundColor = low_color; } }
 .button2{ margin-right: 0; font-size: 16px; display: inline-block; width: 100px; text-align: center; }
 <span class="button2" data-skill="range2">Some Text</span>

您可以使用純 CSS 並轉義選擇器中的括號並使用 nth-child 指定更改背景顏色的元素。

 [\[innerHTML\]='resultSkill.range1']:nth-child(1) { background: red; } [\[innerHTML\]='resultSkill.range1']:nth-child(2) { background: orange; }
 <span class="button-align2" [innerHTML]="resultSkill.range1">red</span> <span class="button-align2" [innerHTML]="resultSkill.range1">orange</span>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM