输入聚焦问题

时间:2010-04-07 20:38:45

标签: php html

这是问题,无论如何我无法承担。 我有以下简单的脚本


    <input class="input"  type="text" name="l_username" style="color: #ccc;" 
     value= " <?if ($_POST[l_username] != '') 
              echo $_POST[l_username];
              else echo 'something';?>"   
     onfocus="if (this.value == 'something') {
 this.value='';this.style.color='black';}" />

onfocus在这里不起作用,但是当我从值中删除php脚本时,它可以正常工作


<input class="input"  type="text" name="l_username" style="color: #ccc;" 
         value= " something"   
         onfocus="if (this.value == 'something') {
     this.value='';this.style.color='black';}" />

它工作正常。 你能告诉我为什么吗? 感谢

2 个答案:

答案 0 :(得分:1)

 " <?if ($_POST[l_username] != '') 
              echo $_POST[l_username];
              else echo 'something';?>" 

因为&lt;?

之前的空格字符

在焦点事件处理程序中,您可以使用defaultValue属性,因此您不必重复两次“something”字符串

onfocus="if(this.value === this.defaultValue){this.value='';this.style.backgroundColor='black';}"

答案 1 :(得分:1)

<input class="input"  type="text" name="l_username" style="color: #ccc;" 
 value= " <?if ($_POST[l_username] != '') 
          echo $_POST[l_username];
          else echo 'something';?>"   
 onfocus="if (this.value == 'something') {
this.value='';this.style.color='black';}" />

你的PHP脚本前面有一个空格。如果情况(if (this.value == 'something'

,请尝试删除它以匹配javascript
相关问题