简体   繁体   English

由于“模糊”而未触发“点击”事件

[英]“Click” event not getting triggered due to “blur”

To reproduce the issue, use the fiddle at [1] and follow these steps: 要重现该问题,请使用[1]中的小提琴并按照以下步骤操作:

  1. Click on text box 点击文本框

  2. Input some value in it 在其中输入一些值

  3. After value input, click of the "click me" button. 输入值后,单击“单击我”按钮。 Please Note, don't click anywhere else on the browser 请注意,请勿在浏览器上单击其他任何地方

  4. You would see the "button click" event not getting triggered. 您将看到未触发“按钮单击”事件。

The HTML code looks like this, HTML代码看起来像这样,

   <div class="wrapper">
    <input type="text" id="input"/>
    <div class="error">There is an error </div>
    </div>
    <button type="button" id="button">Click Me</button>
    <div id="log">Logs</div>

The JavaScript code for the same is: 相同的JavaScript代码是:

$(function () {
   $("input,button").on("click focus blur mousedown mouseup", function (e) {
     $("#log").append($("<div/>").html(e.target.id + " " + e.type))
     var self = this     
     if (self.id === "input" && e.type=="blur") {
       $(self).trigger("exit")
     }      
   })
   $(".wrapper").on("exit", function () {         
      $(this).find(".error").hide()
      $(this).find(".error").text("")
   })
})

The issue is reproducible in "Chrome" and "firefox". 此问题在“ Chrome”和“ firefox”中可重现。 Is this a know bug in "Chrome" or anyone who have faced any similar issue ? 这是“ Chrome”中的已知错误,还是遇到过类似问题的任何人?

Ideally, the click event on button has to be triggered but somehow it doesn't ? 理想情况下,必须触发按钮上的click事件,但是不触发它? I am not able to understand the cause or a possible fix. 我无法理解原因或可能的解决方法。

I don't want to use the setTimeout() to defer the "blur" event execution 我不想使用setTimeout()来推迟“模糊”事件的执行

[1] https://jsfiddle.net/cg1j70vb/1/ [1] https://jsfiddle.net/cg1j70vb/1/

This is happening since on blur of the input you are removing the error text. 发生这种情况是因为在input blur时,您正在删除error文本。 That shifts the button up (possibly DOM re-paint) and hence misses the click 这会使button上移(可能是DOM重新绘制),因此错过了click

I removed the error message and it works fine. 我删除了错误消息,并且工作正常。

 $(function() { $("input,button").on("click focus blur mousedown mouseup", function(e) { $("#log").append($("<div/>").html(e.target.id + " " + e.type)) if (this.id === "input" && e.type == "blur") { $(this).trigger("exit") } }) $(".wrapper").on("exit", function() { $(this).find(".error").hide() $(this).find(".error").text("") }) }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="wrapper"> <input type="text" id="input" /> </div> <button type="button" id="button">Click Me</button> <div id="log">Logs</div> 

I think its because the hide() function of your error message. 我认为是因为您的错误消息的hide()函数。

I tried to remove it and just replace the error message and it works. 我试图将其删除,而只是替换错误消息,它可以正常工作。 Demo 演示版

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM