[英]How to change background color of TextBox by Javascript?
我正在嘗試使用javascript更改文本框的背景顏色,但是我的代碼無法正常工作。 我這樣搜索,但找不到任何合適的答案。 這是我的代碼。
<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屬性。
這里是可通過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不存在。
客戶端上不存在BackColor
。 這就是ASP.NET服務器端的概念。 相反,您想使用CSS進行設置:
v.style.backgroundColor = 'Red';
這是CSS屬性名稱的引用,就像它們在JavaScript中一樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.