当HTML元素获得焦点时执行脚本?

原创
ithorizon 7个月前 (10-19) 阅读数 51 #HTML

使用 onfocus 属性在 HTML 中获得焦点时执行脚本。

示例

您可以尝试运行以下代码来实现 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>

以上就是当HTML元素获得焦点时执行脚本?的详细内容,更多请关注IT视界其它相关文章!



热门