无法获取的未定义或为空引用属性“风格” [英] Unable to get property 'style' of undefined or null reference

查看:269
本文介绍了无法获取的未定义或为空引用属性“风格”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找在网上为我的文本框水印效果得到一些帮助,并发现了一张code看起来像如下:

I have been looking online for a Watermark effect for my textboxes to get some help and found a piece of code which looks like the following :

的Javascript:

function WaterMark(objtxt, event) {
var defaultText = "Username";
var defaultpwdText = "Password";
// Condition to check textbox length and event type
if (objtxt.id == "tb_Username" || objtxt.id == "tb_Password") {
    if (objtxt.value.length == 0 & event.type == "blur") {
        //if condition true then setting text color and default text in textbox
        if (objtxt.id == "tb_Username") {
            objtxt.style.color = "Gray";
            objtxt.value = defaultText;
        }
        if (objtxt.id == "tb_Password") {
            document.getElementById("<%= tb_TempPassword.ClientID %>").style.display = "block";
            objtxt.style.display = "none";
        }
    }
}
// Condition to check textbox value and event type
if ((objtxt.value == defaultText || objtxt.value == defaultpwdText) & event.type == "focus") {
    if (objtxt.id == "tb_Username") {
        objtxt.style.color = "black";
        objtxt.value = "";
    }
    if (objtxt.id == "tb_TempPassword") {
        objtxt.style.display = "none";
        document.getElementById("<%= tb_Password.ClientID %>").style.display = "";
        document.getElementById("<%= tb_Password.ClientID %>").focus();
    }
}

}

然后将HTML

<asp:TextBox runat="server" ID="tb_Username" Text="Username" onblur="WaterMark(this, event);" onfocus="WaterMark(this, event);" />

<asp:TextBox runat="server" ID="tb_TempPassword" Text="Password" onfocus="WaterMark(this, event);" ForeColor="Gray" />

<asp:TextBox runat="server" ID="tb_Password" TextMode="Password" text="Password" Style="display:none" onblur="WaterMark(this, event);"/>

但由于某些原因,当我运行我的code中的用户名框工作正常,但密码箱想出了一个错误说:

But for some reason when i run my code the Username box works fine but the password box comes up with an error saying:

0x800a138f - JavaScript的运行时错误:无法获取的未定义或为空引用属性风格

我一直在寻找在线的修复并没有什么工作?
这是因为我的文本框可以链接至CSS样式表?

I have been looking online for a fix and nothing is working? Is this because my Textbox is linking to a CSS stylesheet?

任何帮助将大大AP preciated。

Any help would be greatly appreciated.

推荐答案

试试这个..

    var id=objtxt.id.toString();
    document.getElementById(id).setAttribute("style", "color:Gray");

这篇关于无法获取的未定义或为空引用属性“风格”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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