[英]Unable to change specific text color in javascript
Here is my code. 这是我的代码。 It's a simple function and what I need to change the text color when it shows
Active
eventStatus: 这是一个简单的函数,当我显示
Active
eventStatus时,我需要更改文本颜色:
function() {
var diff, status;
diff = moment().diff(date, 'days')
if (diff > 0) {
status = 'Ended';
}
if (diff < 0) {
status = 'Pending';
}
if (diff === 0) {
status = 'Active';
//status.style.color = "#132111"
// document.getElementById('greenActive').style.color = "green"
}
return status;
}
UPDATE HTML: 更新 HTML:
<td id = "status-column" >
<span id="greenActive" >{{eventStatus}}</span>
</td>
I tried to use both 'status.style.color' and 'getElementById'. 我试图同时使用“ status.style.color”和“ getElementById”。 The result is that the data/text won't show up at all.
结果是数据/文本根本不会显示。
Anything I did wrong? 我做错了什么?
Thank you in advance for your help! 预先感谢您的帮助!
Edit: 编辑:
Your code working but as I said, you need to make sure to execute the code after DOM loaded 您的代码正常工作,但正如我所说,您需要确保在加载DOM之后执行代码
(function() { var diff, status; diff = 0 if (diff > 0) { status = 'Ended'; } if (diff < 0) { status = 'Pending'; } if (diff === 0) { status = 'Active'; document.getElementById('greenActive').style.color = "green" } return status; })();
<td id = "status-column" > <span id="greenActive" >{{eventStatus}}</span> </td>
You need to make sure that document loaded and DOM element you are trying to modify is available before calling you function. 在调用函数之前,需要确保已加载的文档和要修改的DOM元素可用。 You can see a demo of js modifying text colour working here
您可以在此处看到js修改文本颜色的演示
function() {
var diff, status , statusColor = '';;
diff = moment().diff(date, 'days')
if (diff > 0) {
status = 'Ended';
}
if (diff < 0) {
status = 'Pending';
}
if (diff === 0) {
status = 'Active';
statusColor = '#5CB85C';
//status.style.color = "#132111"
// document.getElementById('greenActive').style.color = "green"
}
return <div style="background-color:'+statusColor+'">'+status+'</div>;
} }
<td id = "status-column" >
<span id="greenActive" >{{eventStatus}}</span>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.