简体   繁体   English

通过JavaScript更改ASP:TEXTBOX的BackgroundColor

[英]Change BackgroundColor of ASP:TEXTBOX via javascript

I have declared asp:textbox in my aspx page. 我在aspx页面中声明了asp:textbox。

 <asp:TextBox ID="txta" runat="server" size="12" value="123"></asp:TextBox>

and I have a javascript function in HEADER: 而且我在HEADER中有一个javascript函数:

<head runat="server">
<title>My Page</title>

<script type = "text/javascript" >

    function changeColor() {
        document.getElementById('<%= txta.ClientID %>').backgroundColor="#FF0000";
        alert( '<%= txta.ClientID %>');
    }
</script>

</head>

on Page_Load event I joined onBlur attribute with asp control like this: 在Page_Load事件中,我使用如下asp控件加入了onBlur属性:

 txta.Attributes.Add("onblur", "changeColor();")

When control losts focus, alert is shown, but backgroundcolor doesn't changed. 当控件失去焦点时,会显示警报,但背景色不会改变。

How to achieve this? 如何实现呢?

Use document.getElementById('<%= txta.ClientID %>').style.backgroundColor = "#FF0000"; 使用document.getElementById('<%= txta.ClientID %>').style.backgroundColor = "#FF0000"; .

You forgot the .style . 您忘记了.style

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM