如果包含“-”,则更改ASP.Net标签的文本颜色

时间:2018-08-22 09:55:00

标签: javascript asp.net

我正在尝试更改ASP.net标签的文本/字体颜色(如果其中包含“-”符号)。

这是一个百分比更改标签,因此负数必须为绿色,正数为红色。

我不断收到 TypeError:document.getElementById(....)为空

我知道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")
};

1 个答案:

答案 0 :(得分:2)

您必须使用ClientID

var change = document.getElementById("<%= lblPercentageDifferenceToFillReqCurrentVsPreviousMonth.ClientID %>").value;

假设它是一个实际的控件,例如

<asp:TextBox ID="lblPercentageDifferenceToFillReqCurrentVsPreviousMonth" runat="server"></asp:TextBox>
相关问题