首页 > 文章列表 > 当HTML元素失去焦点时执行脚本?

当HTML元素失去焦点时执行脚本?

162 2023-09-13

使用 onblur 属性在 HTML 中的元素失去焦点时执行脚本。您可以尝试运行以下代码来实现onblur属性 -

示例

<!DOCTYPE html>
<html>
   <body>
      <p>Type text below!</p>

      Country: <input type = "text" name = "myid" id = "myid" onblur = "display()">

      <script>
         function display() {
            var str = document.getElementById("myid");
            str.value = str.value.toUpperCase();
         }
      </script>

   </body>
</html>