[英]Display text in Text-box
I am working around display text in Text-box & using following code 我正在解决在文本框中显示文本并使用以下代码
<input name="keyword" type="text" value="no spaces, or hyphens" onblur="this.value=(this.value=="") ? 'no spaces, or hyphens' : this.value;" onfocus="this.value=(this.value=='no spaces, or hyphens') ? "" : this.value;" class="txtfield">
but when I click in text-box it gives following error: 但是当我在文本框中单击时,出现以下错误:
Timestamp: 7/17/2013 12:45:19 PM
时间戳记:2013年7月17日下午12:45:19
Error: SyntaxError: syntax error
错误:SyntaxError:语法错误
Line: 1, Column: 51
行:1,列:51
Source Code: 源代码:
this.value=(this.value=='no spaces, or hyphens') ?
Why does this error occur? 为什么会发生此错误?
You either need to use single quotes, or escape the double quotes. 您要么使用单引号,要么转义双引号。
Single Quotes 单引号
<input name="keyword" type="text" value="no spaces, or hyphens" onblur="this.value = (this.value == '') ? 'no spaces, or hyphens' : this.value;" onfocus="this.value = (this.value == 'no spaces, or hyphens') ? '' : this.value;" class="txtfield">
Escaped Double Quotes 转义的双引号
<input name="keyword" type="text" value="no spaces, or hyphens" onblur="this.value = (this.value == \"\") ? \"no spaces, or hyphens\" : this.value;" onfocus="this.value = (this.value == \"no spaces, or hyphens\") ? \"\" : this.value;" class="txtfield">
The more elegant solution is clearly single quotes. 更优雅的解决方案显然是单引号。
<input name="keyword" type="text" value="no spaces, or hyphens" onblur="if(this.value =='') {this.value='no spaces, or hyphens'}" onfocus="if(this.value =='no spaces, or hyphens') {this.value=''}" class="txtfield">
与报价相关的错误,您的onblur属性应该是这样的
onblur="this.value=(this.value=='') ? 'no spaces, or hyphens' : this.value;"
按照以下instedof javascript使用Placeholder html5属性。
<input name="keyword" type="text" value="" placeholder="no spaces, or hyphens" class="txtfield">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.