JavaScript 中 onfocus 事件的用法是什么?

htmljavascriptprogramming scripts更新于 2026/1/14 2:20:17

元素获得焦点时会触发 onfocus 事件。您可以尝试运行以下代码来了解如何在 JavaScript 中实现 onfocus  事件。

示例

<!DOCTYPE html>
<html>
   <body>
      <p>Write below:</p>
      <input type = "text" onfocus = "newFunc(this)">
      <script>
         function newFunc(x) {
            x.style.background = "blue";
         }
      </script>
   </body>
</html>

相关文章


有用资源