【问题标题】:Chrome fires OnBlur again and againChrome 一次又一次地触发 OnBlur
【发布时间】:2018-06-01 15:00:43
【问题描述】:

这是仅在 chrome 中一次又一次触发 Onblur 的代码,在 IE 中运行良好。在 Firefox 中,问题出现在创建两个输入元素时......有人可以帮我解决这个问题吗?我想要关于模糊的警报(在实际代码中,我们检查表中的重复项并在发现重复项时发出警报,此代码只是一个示例,表明我的需要)并在单击“确定”后警告框,我想重新获得焦点。 请查看 IE 中的正确行为。

  $(document).ready(function() {
    $("button").click(function() {
      $("div").append('<input type="text" name="txtCompanyName" maxlength="100" class="Company" style="margin-bottom:0px;width:170px"/>')
    });

    // With on():

    $("div").on("blur", ".Company", function() {
      alert($(this).text());
      $(".Company").focus();
    });
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<html>

<head>
</head>

<body>

  <div></div>
  <button>generate new element</button>

</body>

</html>

有趣的事情- 当 chrome 开始重复触发事件并且您厌倦了一次又一次按“确定”时,然后移动到 Chrome 中的其他选项卡,然后返回此选项卡单击“确定” “,对于那个模糊的例子,问题得到了解决。魔法??

【问题讨论】:

  • 通过将焦点放回模糊处理程序中模糊的元素,您正在创建一个无限循环。

标签: javascript jquery html google-chrome firefox


【解决方案1】:
$("div").on("blur", ".Company", function() {
  alert($(this).text());
  $(".Company").focus();
})

焦点和模糊实际上是相反的。因此,如果您的焦点偏离(模糊),那么您将其焦点设置回公司,并且公司被附加到一个 div 中。所以它无限循环。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-22
    • 1970-01-01
    • 1970-01-01
    • 2021-11-14
    • 2018-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多