第一句子网 - 唯美句子、句子迷、好句子大全
第一句子网 > onBlur和onfocus事件

onBlur和onfocus事件

时间:2024-07-19 20:34:44

相关推荐

onBlur和onfocus事件

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

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

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

例子:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="/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>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。