繁体   English   中英

在文本框中显示文本

[英]Display text in Text-box

我正在解决在文本框中显示文本并使用以下代码

<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">

但是当我在文本框中单击时,出现以下错误:

时间戳记:2013年7月17日下午12:45:19

错误:SyntaxError:语法错误

行:1,列:51

源代码:

   this.value=(this.value=='no spaces, or hyphens') ? 

为什么会发生此错误?

您要么使用单引号,要么转义双引号。

单引号

<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">

转义的双引号

<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">

更优雅的解决方案显然是单引号。

<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.

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