代码示例:(标识:eg_js_events_onfocus)
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction(x) {
  x.style.background = "yellow";
}
</script>
</head>
<body>

请输入您的姓名:<input type="text" onfocus="myFunction(this)">

<p>当输入字段获得焦点时,将触发一个更改背景颜色的函数。</p>

</body>
</html>
运行结果: