Javascript 如何通过Javascript更改TextBox的背景颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8329490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change background color of TextBox by Javascript?
提问by jams
I am trying to change background color of textbox by using javascript but my code is not working. I search SO but not find any suitable answer. Here is my code.
我正在尝试使用 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>
回答by Galled
You are trying to change the UI that is html, so you need to use javascript/css properties.
您正在尝试更改 html 的 UI,因此您需要使用 javascript/css 属性。
Herethere is a list of css attributes accessible by javascript.
这里有一个 javascript 可访问的 css 属性列表。
Try with:
尝试:
<script type="text/javascript" language="javascript">
function abc() {
var v = document.getElementById("<%=TextBox1.ClientID%>");
v.style.backgroundColor = "red";
}
</script>
Furthermore I have the Visual Studio 2010 and the Intellisense also show me the style
attribute:
此外,我有 Visual Studio 2010,Intellisense 还向我展示了该style
属性:
You are right jams, when I pretend to point to an id
from a html generated by asp the intellisense doesn't works for the style
attribute:
你是对的果酱,当我假装id
从由 asp 生成的 html 中指向一个时,智能感知不适用于该style
属性:
I think the Intellisense doesn't reach to this id
because at the moment of you are writting this code, the html doesn't exists.
我认为 Intellisense 没有达到这一点,id
因为在您编写此代码的那一刻,html 不存在。
回答by vcsjones
BackColor
does not exist on the client side. That's an ASP.NET server-side notion. Rather, you want to set it with CSS:
BackColor
在客户端不存在。这是 ASP.NET 服务器端的概念。相反,你想用 CSS 设置它:
v.style.backgroundColor = 'Red';
Hereis a reference of the names of CSS properties as they would appear in JavaScript.
这是 CSS 属性名称的参考,它们将出现在 JavaScript 中。