我想添加'紅'的顏色,而價值的負面和'綠色'的正面價值的顏色。我試了很多想法,爲什麼它不工作,但沒有取得成功。JavaScript和CSS - 我是這樣做的嗎?
請更正我的腳本。下面的代碼有什麼問題:
<!DOCTYPE html>
<html>
<head>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$(".status")
.filter(function() {
return $(this).html() < 0;
})
.parent().css('color', 'blue');
});
</script>
</head>
<body>
<span class="status">10</span><br />
<span class="status">-1</span>
</body>
</html>
這會認爲0爲負值。 –
解釋爲什麼OP代碼無法正常工作,以及爲什麼您的操作的關鍵點(例如,有多個具有ID狀態的項目,以便您需要遍歷它們)將改進您的答案。 – Dijkgraaf