jQuery模糊问题

时间:2018-02-15 06:42:08

标签: javascript jquery

我有两个元素firstName和lastName。当关注两个元素时,我需要提醒一些文本。我使用了jQuery,如下所示。

$(document).on("blur", "#fName", function(event) {
  alert("First Name lost the focus");
});

$(document).on("blur", "#lName", function(event) {
  alert("Last Name lost the focus");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="userReg">
  <input type="text" id="fName" name="firstName" />
  <input type="text" id="lName" name="lastName" />
</div>

但问题在于,即使我将第一个元素集中在第二个警报也会提示。

这里有什么问题?

1 个答案:

答案 0 :(得分:1)

&#13;
&#13;
$(document).on("blur", "#fName", function(event) {
  console.log("First Name lost the focus");
});

$(document).on("blur", "#lName", function(event) {
   console.log("Last Name lost the focus");
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="userReg">
  <input type="text" id="fName" name="firstName" />
  <input type="text" id="lName" name="lastName" />
</div>
&#13;
&#13;
&#13;

它可以正常使用&#34; console.log&#34;因为每次警报弹出窗口以及点击它时都会出现提示&#34;确定&#34;按钮,模糊事件肯定会被调用,因为光标仍然在你的输入控件上,你点击按钮。