我正在使用燒瓶來提供 html 服務。在下面的這段代碼中:
{% if terms|length > 1 %}
<div class="term">
{% for term in terms %}
{{ term }}
{% endfor %}
</div>
{% endif %}
terms原本是一個簡單的專案串列['a', 'b', 'c']。
當前div 標記在迭代值時為每個術語添加相同的背景顏色。現在,這些術語變成了具有如下值的元組串列:
('a': 1)
('b': 2)
('c': 3)
我想不同的顏色增加a,b,c基于自己的價值觀:1,2,3。1給出一種顏色,2給出另一種顏色和3另一種顏色。
當前樣式表有一個條目:
div.term {
background-color:mintcream;
}
如何使用 HTML 和 CSS 實作這種效果?
編輯:根據建議,新的 CSS 定義:
div.term.term-count-0 {
background-color:coral;
}
uj5u.com熱心網友回復:
你定義了三個css
div.term.term-count-1 {
background-color:coral;
}
div.term.term-count-2 {
background-color:red;
}
div.term.term-count-3 {
background-color:black;
}
然后,您可以使用以下代碼:
{% if terms|length > 1 %}
{% for term, n in terms %}
<div class="term.term-count-{%n%}">
{{ term }}
</div>
{% endfor %}
{% endif %}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/316755.html
