HTML输入onfocus&的onblur? [英] HTML input onfocus & onblur?

查看:157
本文介绍了HTML输入onfocus&的onblur?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,今天我正在做一个帮手HTML函数。它看起来像这样:

pre $ 函数输入($ 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。'>'。 $'。'< / label>< input type =''。$ type。'name ='。$ name。'id ='。$ name。'/>';






$ b

正如你所看到的,如果你插入一个值会做一些JavaScript,这样当我点击框时,框内的文字就会消失,



问题:我们如何做到这一点当我们不在输入时有价值吗? (请看看stackoverflow上的搜索框,然而,在我们没有指向输入框之后,那个在stackoverflow上的搜索框没有回来?也许是通过使用onblur?我是对的吗?



希望你明白我的意思。

好,因为有些人没有得到我的意思是
,请参阅


当im没有点击它时。


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


即时点击它。


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


当im再次点击它时。


alt text http://img691.imageshack.us/img691/4485/9491802 0.png



应该是


当我没有点击它时。


替代文字http://img39.imageshack.us/img39/4128/48048759.png


<点击它。


替代文字http://img691.imageshack.us/img691/4485/94918020.png


when我不再点击它。


替代文字http://img39.imageshack.us/img39/4128/48048759.png

解决方案

你想要这个

 < input ... 
onfocus =if(this.value == this。 defaultValue)this.value =''
onblur =if(this.value =='')this.value = this.defaultValue/>

更新:一些较新的浏览器只需添加placeholder属性即可完成所需操作:

 < input placeholder =请输入您的名字/> 

以下是根据您的代码的PHP

  echo<<< END 
< label for =$ name> $ lable< / label>
< input type =$ typename =$ nameid =$ namevalue =$ value
onfocus =if(this.value == this.defaultValue)this .value =''
onblur =if(this.value =='')this.value = this.defaultValue/>
END;


ok, today I'm making a helper HTML function. It looks like this:

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 .'" />'; 
  }
}

As you can see, if you insert a value it will do some JavaScript so that when I click the box, the text inside the box will disappear,

Question: How can we make it to have a value when we are not on the input? (please look at the search box on stackoverflow, however the one that is on stackoverflow doesn't come back after we are not pointing at the input box? Maybe by using onblur? Am I right?

Hope you understand what I mean.

ok becouse some of you are not getting what i mean please see

when im not clicking it.

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

when im clicking it.

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

when im not clicking it again.

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

it should be

when im not clicking it.

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

when im clicking it.

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

when im not clicking it again.

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

解决方案

You want this

<input ...
onfocus="if (this.value==this.defaultValue) this.value = ''"
onblur="if (this.value=='') this.value = this.defaultValue" />

Update: Some newer browsers will do what you want simply by adding the placeholder attribute:

<input placeholder="Please enter your name" />

Here is the PHP according to your code

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;

这篇关于HTML输入onfocus&amp;的onblur?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆