我希望有人能帮我解决这个问题,我是个javascript新手。我正在尝试根据HTML文件中的状态更改文本的颜色。
<td id="payment_status_cl" style="text-align: center; height:25px"><%=resultSet.getString("payment_status")%></td>如果我在javascript中插入if/else语句,它将不起作用。但是,如果没有if/else语句,它就可以工作。例如:
此选项仅更改一种状态的颜色:
function f_color(){
if (document.getElementById('payment_status_cl').value = 'Submitted') {
document.getElementById('payment_status_cl').style.color = "Green";}
}
f_color();这不管用。javascript:
<script>
function f_color(){
if (document.getElementById('payment_status_cl').value = 'Submitted') {
document.getElementById('payment_status_cl').style.color = "Green";
}
else if (document.getElementById('payment_status_cl').value = 'Pending') {
document.getElementById('payment_status_cl').style.color = "Orange";
}
else if (document.getElementById('payment_status_cl').value = 'Warning'{
document.getElementById('payment_status_cl').style.color = "Yellow";
}
else if (document.getElementById('payment_status_cl').value = 'Overdue') {
document.getElementById('payment_status_cl').style.color = "Red";
}
}
f_color();
</script>我已经尝试添加div了。
var status = document.getElementById("payment_status_cl");
function fcolor() {
if (status === 'Submitted') {
document.getElementById("centerbox1").style.backgroundColor = '#99C262';
}
else if (status === 'Pending')
{
document.getElementById("centerbox1").style.backgroundColor = '#F8D347';
}
else if (status === 'Warning')
{
document.getElementById("centerbox1").style.backgroundColor = '#FF6C60';
}
}();但它不起作用。我感谢所有给予我的帮助。
发布于 2020-07-19 11:44:32
欢迎来到Stack Overflow!
我相信您希望在元素上使用innerText方法,而不是使用.value。剩下的看起来还不错
if (document.getElementById('payment_status_cl').innerText = 'Submitted') {
document.getElementById('payment_status_cl').style.color = "Green";
}innertText上的MDN文档:https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/innerText
https://stackoverflow.com/questions/62975985
复制相似问题