定义和用法
onfocus 事件在对象获得焦点时发生。
语法
onfocus="SomeJavaScriptCode"
实例
在本例中,当输入框获得焦点时,其背景颜色将改变:
<html>
<head>
<script type="text/javascript">
function setStyle(x)
{
document.getElementById(x).style.background="yellow"
}
</script>
</head>
<body>
First name: <input type="text"
onfocus="setStyle(this.id)" id="fname" />
<br />
Last name: <input type="text"
onfocus="setStyle(this.id)" id="lname" />
</body>
</html>