我为 BSE 和 BSE 股票交易所创建了实时 API,它运行良好并提供实时股票价格。对,我正在使用 SENSEX api 并尝试根据以下要求更改文本颜色:
如果当前时间速率大于之前的秒速率,则为绿色,反之则为红色。
如果当前汇率等于以前的汇率。然后颜色没有变化。因为它是颜色。
这是我的 API 在控制台中的结果:
{lastprice: "37,290.67", changed: "-294.84", open: 37, low: "37,242.85", high: "37,745.44",…}
这是我的AJAX代码:
setInterval(function() {
$.ajax({
type: "post",
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
url: "{{url('live/change_stoke_market_value_live')}}",
data: {
"_token": "{{ csrf_token() }}"
},
dataType: 'json',
success: function(res) {
if (!res) {
alert('Something went wrong')
} else {
console.log(res)
$('#sensex').html(
' <div class="float-right">\n' +
' <i class="mdi mdi-account-multiple widget-icon"></i>\n' +
' </div>\n' +
' <h5 class="text-muted font-weight-normal mt-0" title="Number of Customers">SENSEX</h5>\n' +
' <h4 class="mt-3 mb-3">' + res.lastprice + ' (' + res.changed + ')</h4>\n'
)
if (res.lastprice.indexOf("+") != -1) {
document.getElementById("sensex").className = "greenText";
} else {
document.getElementById("sensex").className = "redText";
}
}
}
});
}, 1000);
.greenText {
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
color: rgb(83, 241, 83);
text-decoration: none;
}
.redText {
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
color: Red;
text-decoration: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="nav-item dropdown">
<a id="sensex"></a>
</li>
我不知道出了什么问题...API 和 RATE 工作正常但是..没有颜色变化。
最佳答案
这最终成为一个非常简单的 css
特异性问题。我们通过将 !important
添加到类中的文本颜色声明来解决问题。
.greenText {
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
color: rgb(83, 241, 83) !important;
text-decoration: none;
}
.redText {
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
color: Red !important;
text-decoration: none;
}
关于html - Stock quote api,无法打印文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52392060/