简体   繁体   English

如果onclick和onselect中的语句在ASP.NET中不起作用

[英]If statements in onclick and onselect not working in ASP.NET

This should be pretty straight forward, but I've been researching for over a day now and can't figure out why this isn't working. 这应该很简单,但是我已经研究了一天多了,无法弄清楚为什么它不起作用。 I'm making a web page in ASP.NET. 我在ASP.NET中制作一个网页。 What I'm trying to do is have a textbox with gray placeholder text that disappears and changes the font color to black when the textbox is clicked or selected. 我想做的是有一个带有灰色占位符文本的文本框,当单击或选择该文本框时,该文本框会消失,并将字体颜色更改为黑色。 Here's my code: 这是我的代码:

<asp:TextBox ID="addEditTextBox1" runat="server" onselect="if(style.color=='#999999'){style.color='Black'; this.value='';}" onclick="if(style.color=='#999999'){style.color='Black';this.value='';}" ForeColor="#999999" Width="250px">Revision #</asp:TextBox>

I've tried changing the "if" statement triggers to 我尝试将“ if”语句触发器更改为

if(style.color.value=='#999999'){...} 

and

if(ForeColor=='#999999'){...} 

and

if(ForeColor.value=='#999999'){...} 

and many other syntaxes but I can't find one that works. 和许多其他语法,但我找不到一种有效的语法。 I do know the onclick and onselect statements are working because if I replace the "==" with a "=" they trip 100% of the time, but that completely negates the whole idea of an "if" statement. 我确实知道onclick和onselect语句是有效的,因为如果我将“ ==”替换为“ =”,它们会在100%的时间内跳闸,但这完全否定了“ if”语句的整个思想。 I'm new to ASP.NET and JavaScript, so maybe there's something obvious I'm missing, but I can't find it anywhere. 我是ASP.NET和JavaScript的新手,所以也许我缺少一些明显的东西,但是我找不到它。 Here's some other information that I didn't think was relevant but may be helpful: 以下是一些我认为不相关但可能会有所帮助的其他信息:

  • Running in Visual Studio Professional 2012 在Visual Studio Professional 2012中运行
  • This is towards the top of the page's source code: 这是页面源代码的顶部:

     <html xmlns="http://www.w3.org/1999/xhtml"> 
  • Target Framework: .NET Framework 4.5. 目标框架:.NET Framework 4.5。

Feel free to inquire if I left anything out. 随时询问我是否有遗漏。 Thanks! 谢谢!

Either just simply use a HTML5 placeholder attribute, all the browsers support it now. 只需使用HTML5 placeholder属性,所有浏览器现在都支持它。

Or use this, works for both keyboard tabbing and mouse clicking: 或使用此方法,可同时用于键盘制表键和鼠标单击:

<asp:TextBox ID="addEditTextBox1" runat="server" onfocus="this.onfocus = null; this.value = ''; this.style.color = 'black';" ForeColor="#999999" Width="250px">Revision #</asp:TextBox>

Btw, onclick is really just for buttons, you probably meant onmousedown and onkeydown - onselect is for when selecting text (to prevent it for example). 顺便说一句, onclick实际上仅用于按钮,您可能意味着onmousedownonkeydown - onselect用于选择文本(例如,防止文本显示)。

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

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