2013-07-17 4 views
0

Я работаю вокруг отображения текста в текстовом поле & используя следующий кодОтображение текста в текстовом поле

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

, но когда я нажимаю в текстовом поле он дает следующее сообщение об ошибке:

Timestamp: 7/17/2013 12:45:19 PM

Error: SyntaxError: syntax error

Line: 1, Column: 51

Исходный код:

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

Зачем возникает эта ошибка?

+0

Вопрос заключается в том, почему возникает ошибка, очевидно ... –

ответ

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

Вам либо нужно использовать одинарные кавычки, либо избежать двойных кавычек.

Одиночные Котировки

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

Более элегантное решение явно одинарные кавычки.

+0

это дает мне SyntaxError: ошибка синтаксиса [BREAK на этой ошибке] \t this.value = (this.value == –

0

Существует связанная ошибка котировка атрибут вашего ONBLUR должно быть, как это

onblur="this.value=(this.value=='') ? 'no spaces, or hyphens' : this.value;" 
0

Использование заполнителем атрибута html5 как следующий instedof JavaScript.

<input name="keyword" type="text" value="" placeholder="no spaces, or hyphens" class="txtfield"> 
Смежные вопросы