如何使用类为目标的控件的ID来改变背景颜色 [英] How to change background color using a class to target the ID of a control

查看:335
本文介绍了如何使用类为目标的控件的ID来改变背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<asp:TextBox ReadOnly="true" ID="tbPhone" ClientIDMode="Static" runat="server" CssClass="tbStyle changeUpdate" Text=""></asp:TextBox>
<asp:TextBox ReadOnly="true" ID="tbFax" ClientIDMode="Static" runat="server" CssClass="tbStyle changeUpdate" Text=""></asp:TextBox>

$('#tbPhone, #tbFax').keypress(function() { //works
    this.style.backgroundColor = "#BCFFB9";
});

我将有很多的文本框,并想用一个类为每个文本框,获得ID和设置背景颜色。这将确保我可以用code的几行对于所有的文本框,无论数量的。

I will have a lot of textboxes and wanted to use a class for each textbox, get the ID and set the background color. This will ensure I can use a few lines of code for all the textboxes, regardless of the number.

所以我尝试这样的:

$(".changeUpdate").keypress(function() {
    $(this).attr("id").style.backgroundColor = "#BCFFB9";
});

不过,我不断收到此错误:

But I keep getting this error:

0x800a138f - Microsoft JScript runtime error: Unable to set value of the property 'backgroundColor': object is null or undefined

如何解决我的问题。

How can I resolve my issue.

推荐答案

您是那种混合Javascript和jQuery的语法,试试这个:

You are kind of mixing Javascript and jQuery syntaxes, try this:

$(".changeUpdate").keypress(function() {
    //$(this).attr("id").style.backgroundColor = "#BCFFB9";
    $(this).css("background-color","#BCFFB9");
});

这篇关于如何使用类为目标的控件的ID来改变背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆