onBlur和onfocus事件

  • Post author:
  • Post category:其他


往往在做网页的时候会遇到要对一些表单控件做一些定制的背景渲染,下面一些关于文本框的onBlur和onfocus事件。此Demo是实现了当文本框获得焦点的时候输入背景为“黄色”,当文本框失去焦点的时候输入背景为“白色”。

onBlur事件:onBlur事件是当光标离开文本框中时发生的事件。

onfocus事件:onfocus事件在对象获得焦点时发生的事件。

例子:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

<html xmlns=”http://www.w3.org/1999/xhtml”>

<head>

<script type=”text/javascript”>

//获得焦点输入背景设置为黄色

function setStyle(x)

{

document.getElementByIdx_x(x).style.background=”yellow”

}

//失去焦点输入背景重置为白色

function recoveryStyle(y)

{

document.getElementByIdx_x(y).style.background=”White”

}

</script>

</head>

<body>

First Name: <input type=”text”

οnfοcus=”setStyle(this.id)” id=”fname” onBlur=”recoveryStyle(this.id)” />

<br />

Last Name: <input type=”text”

οnfοcus=”setStyle(this.id)” id=”lname” onBlur=”recoveryStyle(this.id)”/>

</body>

</html>



版权声明:本文为zhoujiao_2010原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。