在导致模糊的点击事件之后,我应该如何触发 Javascript 模糊事件?
我已经遇到过这个问题几次,并且我对之前使用的解决方案并不满意。
我有一个带有模糊事件的输入框,用于验证其内容,还有一个按钮,单击后将填充输入框。问题是单击按钮会触发输入模糊事件,然后触发按钮单击事件,因此按钮插入的内容不是经过验证的内容。
请参阅 http://jsfiddle.net/jakecr/dL3K3/
我知道这是正确的行为,但我想不出一个干净的方法来解决这个问题。
I have run into this problem a few times and I'm not happy with the solutions I've used before.
I have an input box with a blur event that validates the content of it and a button which will fill the input box on click. The problem is clicking the button fires the input blur event and then the button click event so content inserted by the button is not what is validated.
See http://jsfiddle.net/jakecr/dL3K3/
I know this is the correct behavior but I can't think of a clean way to get around the problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我们在工作中也遇到过类似的问题。我们最终发现了什么
mousedown 事件将在模糊事件之前触发
允许您在验证之前设置内容,甚至取消验证
完全使用标志进行处理。
检查我用你的例子制作的这个小提琴-
http://jsfiddle.net/dL3K3/31/
-编辑-
正如@mclin建议的,使用mousedown 事件和 PreventDefault 将阻止模糊行为。
您可以完整保留原始点击事件 -
http://jsfiddle.net/dL3K3/364/
对于大多数情况,此解决方案可能更干净、更好,只需注意,如果使用它,您将防止当前焦点所在的任何其他元素的默认和模糊,但对于大多数用例来说,这不应该成为问题。
We had a similar problem at work. what we figured out in the end
was that the mousedown event will fire before the blur event
allowing you to set the content before the validation or even cancel the validation
process completely using a flag.
check this fiddle I made using your example-
http://jsfiddle.net/dL3K3/31/
-Edit-
As @mclin Suggested, using the mousedown event and preventDefault will prevent blur behavior.
And you can just leave the original click event intact -
http://jsfiddle.net/dL3K3/364/
This solution might be a bit cleaner and better for most cases just note that if you use it you'll preventDefault and blur of any other element the focus is currently on but for most use cases that shouldn't be an issue.
我有一个比 Yoni Jah 更好的解决方案:
在按钮的 mousedown 事件上调用 PreventDefault。 onBlur 永远不会发生,因此您可以在点击事件中自由地执行任何操作。
这更好,因为它不会通过使事件发生在鼠标按下而不是鼠标松开时来改变单击行为,这可能是意外的。
I have a better solution than Yoni Jah's:
call preventDefault on the button's mousedown event. The onBlur never happens so you're free to do whatever in the click event.
This is better because it doesn't change the click behavior by making things happen on mouse down instead of mouse up, which can be unexpected.
这也可能有帮助:
设置一个计时器,在触发按钮单击事件之前延迟模糊事件操作。
This might also help:
Set a timer that delays the blur event action before it fires the button click event.
将输入验证逻辑与其自己的函数分开,该函数由模糊事件自动调用,然后在修改输入框中的值后由单击事件调用。确实,您的验证逻辑将被调用两次,但我没有看到在不进行更多更改的情况下解决该问题的方法。
使用 jQuery 的示例:
Separate the input validation logic to it's own function which is called both by the blur event automatically and then by the click event after you've modified the value in the input box. True your validation logic would be called twice, but I'm not seeing a way around that without making a lot more changes.
Example using jQuery:
有时,在鼠标按下时获取子对象很有用,但父对象希望在模糊时隐藏其子对象。我们可以取消子元素的隐藏,然后等到 mouseup 发生,然后在准备好时强制发生模糊
js
html
css
https://jsfiddle.net/tic84/on421rxg/
单击列表项只会在鼠标松开时发出警报,尽管父母试图将它们隐藏在模糊中
Sometimes it is useful to get a child object on mouseup, but the parent wants to hide its children on blur. We can cancel the hiding of the children elements, then wait until our mouseup occurs, then force the blur to occur when we are ready
js
html
css
https://jsfiddle.net/tic84/on421rxg/
Clicking the list items will only alert on mouseup, despite the parent trying to hide them on blur
诀窍是不要使用模糊和点击事件,因为它们总是按照先模糊、再点击的顺序执行。相反,您应该检查具有焦点的元素何时发生更改,因为这种情况仅在单击后发生。
我想我有一个优雅的解决方案:
现在所有现代浏览器都支持 document.activeElement。
The trick is not to use blur and click events, because they are always performed in order of blur first, click second. Instead you should check when the element having focus has changed, because that happens only after the click.
I think I have an elegant solution for this one:
These days all modern browsers support document.activeElement.