我正在尝试使用 javascript 更改文本框的背景颜色,但我的代码无法正常工作。我搜索 SO 但没有找到任何合适的答案。这是我的代码。
<head>
<script type="text/javascript" language="javascript">
function abc() {
var v = document.getElementById("<%=TextBox1.ClientID%>");
v.setAttribute('BackColor', 'Red');
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="abc()" onclick="Button1_Click"/>
</div>
</form>
</body>
最佳答案
您正在尝试更改 html 的 UI,因此您需要使用 javascript/css 属性。
Here有一个 JavaScript 可访问的 css 属性列表。
尝试:
<script type="text/javascript" language="javascript">
function abc() {
var v = document.getElementById("<%=TextBox1.ClientID%>");
v.style.backgroundColor = "red";
}
</script>
此外,我有 Visual Studio 2010,Intellisense 还显示了 style
属性:
你是对的,当我假装从 asp 生成的 html 中指向 id
时,intellisense 不适用于 style
属性:
我认为 Intellisense 没有到达这个 id
因为在你写这段代码的时候,html 不存在。
关于javascript - 如何通过 Javascript 更改 TextBox 的背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8329490/