jquery定义自己的事件?

Hai*_*ood 3 jquery events blur settimeout

是否可以在这些情况下触发的输入上定义事件.

a) is called on blur of input IF the remaining conditions are true
b) if the autocomplete list is visible halt the event until it is closed
c) if the autocomplete list closes without an item being selected then the event is fired
d) if the autocomplete list closes with an item being selected the event is not fired
e) if the reason the blur was caused was because an item in the autocomplete was clicked the event is not fired

正如你所看到的那样,事件有很多.我无法在模糊事件中使用正常的setTimeout,因为用户可能坐在自动完成列表上而没有实际选择任何内容.

也许我可以在自动完成打开时设置一个变量,所以我们知道它仍然是开放的.
如果计时器到期且自动完成仍然打开,我们可以重置计时器.
然后在关闭时我们可以取消设置变量.
或者在选择项目时我们可以取消定时器?

你怎么看?

Exe*_*ian 5

可以使用Trigger功能触发您自己的事件.这意味着您可以在代码中的不同位置调用事件,因此您不必编写复杂的构造.

只需这样打电话: $('.yourClass').trigger('myEvent');

链接到此事件可以通过.bind(),即.$('.yourClass').bind('myEvent', function () { });