[英]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.