試圖替換django項目中呈現的每個項目的百分比圓的筆觸顏色。到目前爲止,我只設法將顏色更改爲紅色。我懷疑這是因爲它根據檢索到的第一個/最後一個記錄(百分比),將它們更改爲全部。jQuery - 根據數值更改顏色並逐個遍歷每個項目
有沒有一種方法來遍歷jQuery中的每個項目,以便每個項目的筆觸顏色會改變?
的jQuery:
$(function() {
var score = parseInt($("#percentage").text());
if (score <=40) {
$("path").css("color", "red")
} else if (score >=40) {
$("path").css("color", "green")
}
});
HTML:
{% extends "base.html" %}
{% load bootstrap_tags %}
{% load staticfiles %}
{% block head_js %}
<script src="{% static "js/percentage.js" %}"></script>
{% endblock %}
{% block content %}
{% for statistic in statistics %}
<div class="stat_img">
<img width="100%" src="/media/{{statistic.image}}">
</div>
<span id="percentage">{{statistic.percentage}}</span>
<div class="stat_ranking">
<span class="stat_title">{{statistic.title}} ({{statistic.year}})</span>
<br>
<svg viewbox="0 0 36 36" class="circular-chart">
<path class="circle" stroke-dasharray="{{statistic.percentage}}, 100"
d="M18 2.0845 a 15.9155 15.9155 0 0 1 0 31.831
a 15.9155 15.9155 0 0 1 0 -31.831"/>
<text x="50%" y="40%" text-anchor="middle" dy=".3em">{{statistic.percentage}}%</text>
<text class="blue" x="50%" y="55%" text-anchor="middle" dy=".3em">{{statistic.rating}}</text>
</svg>
</div>
<div class="statistics">
Genre: {{statistic.genre}} <br>
Box Office: ${{statistic.box_office}} <br>
Budget: ${{statistic.budget}}
</div>
<hr>
{% endfor %}
{% endblock %}
想知道什麼路徑? –
如果你分享你的html代碼,它會更清楚地回答它。 – Jayground