Jquery plugins jQuery:自定义事件的触发器函数

Jquery plugins jQuery:自定义事件的触发器函数,jquery-plugins,jquery,Jquery Plugins,Jquery,我创建了一个名为“onEnter”的jQuery函数。 它在跑步时效果很好,但我需要随时触发活动。有人这样想: $(obj).onEnter(function(){ doSomething(); }); 这很有魅力。 然后我想触发: $(obj).onEnter(); 但这似乎不起作用。 这是我的密码。 提前谢谢 $.fn.extend({ onEnter: function(fn) { this.each(function() { new $

我创建了一个名为“onEnter”的jQuery函数。 它在跑步时效果很好,但我需要随时触发活动。有人这样想:

$(obj).onEnter(function(){ doSomething(); });
这很有魅力。 然后我想触发:

$(obj).onEnter();
但这似乎不起作用。 这是我的密码。 提前谢谢

$.fn.extend({
    onEnter: function(fn) {
        this.each(function() {
            new $.onEnter( this, fn );
        });
        return this;
    }
});

$.onEnter = function( obj, fn ) {
    if(typeof fn == 'function')
    {
        $(obj).keypress(function(e){
            if(e.which == 13)
            {
                e.preventDefault();
                fn(obj);
            }
        });
    };
    return;
};

我想你是想用它

所以你可以这样做:

$('#foo').bind('custom', function(event, param1, param2) 
{
  alert(param1 + "\n" + param2);
});

$('#foo').trigger('custom', ['Custom', 'Event']);

我想你是想用它

所以你可以这样做:

$('#foo').bind('custom', function(event, param1, param2) 
{
  alert(param1 + "\n" + param2);
});

$('#foo').trigger('custom', ['Custom', 'Event']);

为什么要使用
new
?为什么要使用
new