getElementById成功一次,然后返回null [英] getElementById succeeds once then returns null

查看:75
本文介绍了getElementById成功一次,然后返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是javascript的新手,这真让我发疯. 我试图根据标志("IsValid")的值设置标签("lblerrmsg")的文本和颜色.我已经在.js文件中编写了一个函数,并将其附加到使用VS构建的网站上.

I'm new to javascript and this is driving me nuts. I'm attempting to set the text and color of a label ("lblerrmsg") depending upon the value of a flag ("IsValid"). I've written a function in a .js file and have attached it to web site that I built with VS.

该函数-尤其是 getElementById('lblErrMsg'),在首次调用时可以正常工作,但在随后的调用中将返回null. (不知道这是否相关-但是在调用该函数之间没有任何帖子.)

The function - specifically getElementById('lblErrMsg') works correctly the first time it is called, but in subsquent calls it returns null. (Don't know if this is relevant - but there are no posts between calls to the function.)

以下是该功能的相关部分:

Following is the relevant portion of the function:

// If IsValid is false - make the text red
var ErrMsg = document.getElementById('lblErrMsg');
if (IsValid) {
    document.activeElement.style.color = 'navy';
    ErrMsg.outerHTML = 'valid';
}
else {
    document.activeElement.style.color = 'red';
    ErrMsg.outerHTML = "*** Invalid Entry ***";
    ErrMsg.style.color = 'red';
}


<asp:TextBox ID="tbNumber" runat="server"></asp:TextBox>
<asp:Label ID="lblErrMsg" runat="server" ForeColor="Red" 
    Text="   xxx" ></asp:Label>

推荐答案

如果需要,尝试更改div或元素的类名称以更改css样式

try to change the class name of the div or element to change the css style if you want

  document.getElementById("blah").className = "cssclass";

通过这种方式,您可以根据自己的标志控制CSS.

this way you can control the css depending on the flag of yours.

这篇关于getElementById成功一次,然后返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆