在外部点击时警报不断出现 chrome

Alert Comes Continuously when clicks outside chrome

我在文本框上使用 onblur 并显示警告。但是,当我在文本框中输入内容后单击 chrome 浏览器外部,然后单击任务栏中的 chrome 浏览器按钮时,警报会持续出现。再次单击 chrome 浏览器按钮后,警报将只出现一次。

这是我的代码:

<!DOCTYPE html>
<html>
<body>
  Enter your name: <input type="text" id="fname" onblur="myFunction()">

  <p>When you leave the input field, a function is triggered which transforms the input text to upper case.</p>

  <script>
    function myFunction() {
      var x = document.getElementById("fname");
      alert(x.value.toUpperCase());
    }
  </script>
</body>
</html>

根据您的问题,我建议使用 keyup,如下所示:

function myFunction() {
  var x = document.getElementById("fname");
  x.value = x.value.toUpperCase();
}
Enter your name: <input type="text" id="fname" onKeyup="myFunction()">

<p>When you leave the input field, a function is triggered which transforms the input text to upper case.</p>