简体   繁体   中英

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

Error: SyntaxError: syntax error

Line: 1, Column: 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">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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