HTML输入onfocus&onblur?

Ada*_*han 10 html javascript effects onfocus onblur

好的,今天我正在制作一个帮助HTML功能.它看起来像这样:

function Input($name,$type,$lable,$value= null){
  if (isset($value)) {
    //if (this.value=='search') this.value = ''
    echo '<label for="'. $name .'">'. $lable .'</label><input type="'.$type.'" name="'. $name .'" id="'. $name .'" value="'.$value.'" onfocus="if (this.value==\''.$value.'\') this.value = \'\' "/>';  
  }
  if (!isset($value)) {
    echo '<label for="'. $name .'">'. $lable .'</label><input type="'.$type.'" name="'. $name .'" id="'. $name .'" />'; 
  }
}
Run Code Online (Sandbox Code Playgroud)

如您所见,如果您插入一个值,它将执行一些JavaScript,以便当我单击该框时,框内的文本将消失,

问题:当我们没有输入时,我们如何才能使它具有价值?(请查看stackoverflow上的搜索框,但是我们没有指向输入框后,stackoverflow上的搜索框没有回来?也许是通过使用onblur?我是对的吗?

希望你明白我的意思.

好吧因为有些人没有得到我的意思,请看

当我没有点击它.

alt text http://img39.imageshack.us/img39/4128/48048759.png

当我点击它.

alt text http://img691.imageshack.us/img691/4485/94918020.png

当我不再点击它.

alt text http://img691.imageshack.us/img691/4485/94918020.png

它应该是

当我没有点击它.

alt text http://img39.imageshack.us/img39/4128/48048759.png

当我点击它.

alt text http://img691.imageshack.us/img691/4485/94918020.png

当我不再点击它.

alt text http://img39.imageshack.us/img39/4128/48048759.png

mpl*_*jan 29

你要这个

<input ...
onfocus="if (this.value==this.defaultValue) this.value = ''"
onblur="if (this.value=='') this.value = this.defaultValue" />
Run Code Online (Sandbox Code Playgroud)

更新:一些较新的浏览器只需添加占位符属性即可执行您想要的操作:

<input placeholder="Please enter your name" />
Run Code Online (Sandbox Code Playgroud)

这是根据您的代码的PHP

echo <<<END
<label for="$name">$lable</label>
<input type="$type" name="$name" id="$name" value="$value" 
 onfocus="if (this.value==this.defaultValue) this.value = ''" 
 onblur="if (this.value=='') this.value = this.defaultValue"/>
END;
Run Code Online (Sandbox Code Playgroud)