javascript - 如果包含“-”,则更改 ASP.Net 标签文本颜色
问题描述
如果我的 ASP.net 标签包含“-”符号,我正在尝试更改它的文本/字体颜色。
这是一个百分比变化标签,因此负数需要为绿色,正数为红色。
我不断收到TypeError: document.getElementById(....) is null
我知道 window.onLoad 不是最佳实践,这只是为了快速测试它。
谁能告诉我我做错了什么……转圈圈。
window.onload = fillDays;
function fillDays() {
var change = document.getElementById("<%=lblPercentageDifferenceToFillReqCurrentVsPreviousMonth %>").value;
if (change.indexOf(char) = '-') {
document.getElementById("<%=lblPercentageDifferenceToFillReqCurrentVsPreviousMonth %>").style.color = "green";
}
else {
document.getElementById("<%=lblPercentageDifferenceToFillReqCurrentVsPreviousMonth %>").style.color = "red";
}
console.log("fillDays")
};
解决方案
你必须使用ClientID
var change = document.getElementById("<%= lblPercentageDifferenceToFillReqCurrentVsPreviousMonth.ClientID %>").value;
假设它是一个实际的 Control 像
<asp:TextBox ID="lblPercentageDifferenceToFillReqCurrentVsPreviousMonth" runat="server"></asp:TextBox>
推荐阅读
- php - 最近在 laravel 7 中通过 cookie 浏览的产品一次只能保存 1 个产品
- c# - 如何使用 webclient 下载文件使用 memortstream 将它们保存为 gif 类型的图像,同时报告进度?
- r - 根据R中不同数据帧的两个变量的ifelse问题
- javascript - 如何将信息从控制器传递到局部视图
- node.js - 是否可以在 VSCode Jupyter Notebook 中本地运行 node.js?
- crm - 是否可以在 appsettings.json 中发布/更改数据或值?
- powerquery - PowerQuery:强制小写字母数字文本
- google-apps-script - GmailApp.search() 奇怪的行为
- pyspark - 使用 pyspark 流使用 CURE 算法进行聚类
- xamarin - Xamarin Android 不再打印 MONO GC 日志消息