简体   繁体   English

模糊事件会停止点击事件

[英]Blur event stops click event

Example code: http://jsfiddle.net/slolife/PnmxM/ 示例代码: http//jsfiddle.net/slolife/PnmxM/

I am asking this even though there are a number of similar questions, but I feel that they are not the same. 即使有许多类似的问题,我也会问这个问题,但我觉得它们不一样。

I have an textbox that when it is blurred, should do something. 我有一个文本框,当它模糊时,应该做点什么。

I also have a link, that is always visible (that appears to be how the other questions differ) and when clicked, should do something. 我也有一个链接,总是可见的(似乎是其他问题的不同),点击后,应该做一些事情。

My blur handler fires, but the click handler does not. 触发我的模糊处理程序,但点击处理程序不会触发。

Is there a proper way to deal with this? 有没有正确的方法来处理这个?

Update 更新

Many people pointed out that the alerts were causing my problem. 很多人指出警报引起了我的问题。 Thank you. 谢谢。 In my real code, I do not have alerts, but instead remove the textbox from the DOM. 在我的真实代码中,我没有警报,而是从DOM中删除文本框。

So I have updated the fiddle to better reflect that with console.log calls instead of alert() calls. 所以我更新了小提琴以更好地反映使用console.log调用而不是alert()调用。 Any additional help is appreciated. 任何额外的帮助表示赞赏。

This is because of the first alert breaking the second alert because it's modal. 这是因为第一个alert打破了第二个alert因为它是模态的。 See this: 看到这个:

Here, I'm appending the message to msgs div and it works as expected. 在这里,我将消息附加到msgs div,它按预期工作。

For your updated jsFiddle, here's an (updated-updated?) working one: 对于您更新的jsFiddle,这是一个(更新更新?)工作的:

You are removing the input box in your onBlur and, as a consequence of that, moving the Add item vertically, thus the click doesn't happen on Add item anymore (as your mouse pointer did not move in the meantime), but on some other element (in this case, a jsFiddle example container). 您正在删除onBlur的输入框,因此,垂直移动Add item ,因此点击不再发生在Add item (因为您的鼠标指针在此期间没有移动),但是在某些情况下其他元素(在本例中为jsFiddle示例容器)。 Moving Add item above the disappearing input element solves the "click me if you can" issue. 移动Add item在消失的input元素上方解决了“如果可以的话,请点击我”问题。

The click event is not firing because that only happens when you release the mouse button. click事件未触发,因为只有在释放鼠标按钮时才会触发。 That isn't happening because there is a modal dialog window visible (the alert window). 这种情况没有发生,因为可以看到模态对话框窗口(警报窗口)。 If you change the alerts to console.log instead then it works perfectly... 如果您将警报更改为console.log那么它可以完美地运行...

http://jsfiddle.net/PnmxM/7/ http://jsfiddle.net/PnmxM/7/

Because you are using an alert you are interrupting the execution, which causes a problem for me in Firefox - but if you switch to console.log and make sure you have a console (like Firebug) open, you can see both events fire. 因为你正在使用alert你正在中断执行,这会导致我在Firefox中出现问题 - 但如果你切换到console.log并确保打开一个控制台(如Firebug),你可以看到这两个事件都会激活。

function onBlur() {
    console.log('blur');
}

function addItem() {
    console.log('add');
}

$(document).ready(function() {
    $('#items').delegate('input','blur', onBlur);
    $('#addItem').click(addItem);
});

Depending on your requirements, another way to fix this if you can't rearrange your UI is to simply bind to the mousedown event instead of click . 根据您的要求,如果您无法重新排列UI,另一种解决方法是简单地绑定到mousedown事件而不是click I've added my own modification of the console.log() jsFiddle to demonstrate. 我已经添加了我自己的console.log()jsFiddle修改来演示。

I ran into this issue with a menu that showed when the user focuses on a text box, and hides when they blur. 我遇到了这个问题,其中的菜单显示用户何时关注文本框,并在模糊时隐藏。 Clicks in the menu weren't firing in Chrome, because the blur hid the menu on mousedown, so when the mouse came back up the pointer wasn't in the menu any more (because it had disappeared), since that handler then got called before blur hid the menu, and my click events hit their targets. 菜单中的点击没有在Chrome中触发,因为模糊隐藏了mousedown上的菜单,所以当鼠标重新启动时,指针不再出现在菜单中(因为它已经消失),因为该处理程序随后被调用在模糊隐藏菜单之前,我的点击事件击中了他们的目标。

Just try after update the fiddle http://jsfiddle.net/slolife/PnmxM/ . 只需在更新小提琴http://jsfiddle.net/slolife/PnmxM/后再尝试。

Update the onBlur function to this. 将onBlur函数更新为此。

Blur and click will works. 模糊和点击将有效。

 function onBlur() {
        console.log('blur');
        $('#item').val("test");
    }

Current issue is : the click is not triggering because the ui position of "Add item" is changing (because the textfield disappears). 当前的问题是:点击没有触发,因为“添加项目”的ui位置正在改变(因为文本字段消失)。

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

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