代码示例:(标识:event_onfocusin_addeventlistener)
<!DOCTYPE html>
<html>
<body>

<p>本例使用 addEventListener() 方法将 "focusin" 事件附加到 input 元素。</p>

请输入您的姓名:<input type="text" id="fname">

<script>
document.getElementById("fname").addEventListener("focusin", myFunction);

function myFunction() {
  document.getElementById("fname").style.backgroundColor = "red";
}
</script>

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