jquery定义自己的事件?

时间:2011-02-24 08:53:37

标签: jquery events settimeout blur

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

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,因为用户可能坐在自动完成列表上而没有实际选择任何内容。

也许我可以在自动完成时设置一个变量,这样我们就知道它仍处于打开状态 如果计时器到期且自动完成仍然打开,我们可以重置计时器 然后在关闭时我们可以取消变量。
或者在选择项目时我们可以取消定时器?

您怎么看?

2 个答案:

答案 0 :(得分:5)

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

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

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

答案 1 :(得分:0)

您可以使用fling plugin来提升自定义事件并为其添加侦听器。代码看起来像:

$(yourinput).blur(function(){
    if(....your condition....)
        $.fling('publish','YourEventName',your data in json);
}

附加听众:

 $.fling('subscribe','YourEventName', function(){
 });