当元素聚焦于HTML时执行脚本?

当元素在HTML中获得焦点时,请使用onfocus属性执行脚本。

示例

您可以尝试运行以下代码以实现onfocus属性-

<!DOCTYPE html>
<html>
   <body>
      <p>Enter subject name below,</p>
      Subject: <input type = "text" id = "sname" onfocus = "display(this.id)">
      <br>

      <script>
         function display(val) {
            document.getElementById(val).style.background = "blue";
         }
      </script>
   </body>
</html>