我创建了一个名为“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;
};
答案 0 :(得分:0)
我认为您希望使用jQuery .trigger()。
所以你可以这样做:
$('#foo').bind('custom', function(event, param1, param2)
{
alert(param1 + "\n" + param2);
});
$('#foo').trigger('custom', ['Custom', 'Event']);