gpt4 book ai didi

javascript - 无法获取未定义或空引用的属性 'style'

转载 作者:行者123 更新时间:2023-11-28 08:18:14 26 4
gpt4 key购买 nike

我一直在网上寻找文本框的水印效果以获得一些帮助,并找到了一段如下所示的代码:

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);"/>

但由于某种原因,当我运行代码时,用户名框工作正常,但密码框出现错误:

0x800a138f - JavaScript 运行时错误:无法获取未定义或 null 引用的属性“样式”

我一直在网上寻找修复方法,但没有任何效果?这是因为我的文本框链接到 CSS 样式表吗?

任何帮助将不胜感激。

最佳答案

试试这个..

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

关于javascript - 无法获取未定义或空引用的属性 'style',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23339119/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com