Javascript事件处理程序命令 [英] Javascript event handler order

查看:76
本文介绍了Javascript事件处理程序命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输入字段,它有两个绑定到它的事件处理程序。

I have an input field, which has two event handlers bound to it.

验证& AutoSave

Validate & AutoSave

显然我想在保存前验证。如果验证失败,则将无效类添加到输入中,并且自动保存将在该类继续之前检查该类。

Obviously I want to validate before I save. If validation fails, the "invalid" class is added to the input and autosave will check for that class before it proceeds.

这种方法运作良好,但有没有办法保证在所有情况下自动保存之前运行Validate?

This works well enough, but is there a way to guarantee Validate runs before Autosave in all cases?

推荐答案

如果使用JQuery来绑定事件,它可以保证处理程序的触发顺序与它们绑定的顺序相同。否则订单正式未定义。

If you use JQuery to bind your events, it guarantees that handlers are fired in the same order that they were bound. Otherwise the order is officially undefined.

如果你不能使用JQuery或类似的框架,你可以使用自己的自定义偶数绑定轻松模拟这个,你的通用处理程序是函数,它保存一系列函数并按顺序调用它们。

If you cannot use JQuery or a similar framework you can easily simulate this by using your own custom even binding, where your generic handler is a function which keeps an array of functions and calls them in order.

这篇关于Javascript事件处理程序命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆