2012-04-27 46 views
0

所以说我有这个..如何通过params传递客户关键字处理程序?

keyup_handler: function(e, item){ 
    if (e.which == 27) { 
     close_lightbox(item); 
     return false;  
    }; 
    } 

$(document).live('keyup', function(e){ keyup_handler(e, item) }); 

这不是在这里返回任何的console.log()的我的地方。所以我认为它没有被触及。我错过了什么?如何将eevent传递给keyup_handler?

的jQuery 1.4.4

+0

你可以在[jsfiddle](http://jsfiddle.net)中重现这个吗? – 2012-04-27 14:04:04

+1

你能比“不工作”更具体吗?我期望在4.7k代表你现在已经学会了如何提出更好的问题。 – 2012-04-27 14:07:57

+0

我的歉意安东尼。更新。 – Trip 2012-04-27 14:14:07

回答

1

你返回false从呼叫keyup_handler,但不能从调用它的功能,尝试做:

$(document).live('keyup', function(e){ return keyup_handler(e, item); }); 

如果if语句不计算为真,您可能还需要稍微修改keyup_handlerreturn true;

我在这里假设你非常模糊的“不工作”语句意味着当你释放相应的键时,通常的键控功能不会被阻止。

此外,.live()是一个不推荐使用的jQuery函数,并非真正用于您使用它的目的。使用.on()(如果您使用的是jQuery 1.7+),或者使用.bind()

+0

啊谢谢!这工作。我必须投入回报。我的其他处理者不需要这些。非常感谢。 – Trip 2012-04-27 14:14:39

0

在jQuery的在看看keyup()尝试

0

试试这个 - >

$(document).live('keyup', function(e){ 
    if (e.which == 27) { 
close_lightbox(<selector-that selects-item>); 
     return false;  
    }; }); 
0
keyup_handler = function(e, item){ 
    var code = (e.keyCode ? e.keyCode : e.which); 
    if (code == 27) { 
     close_lightbox(item); 
     return false;  
    } 
} 

$(document).on('keyup', 'dynamic_element', function(e){ keyup_handler(e, item) });​ 

FIDDLE

现场()已被弃用,不知道为什么你需要使用它的文件? 对于旧版本的jQuery的,我想这就够了:

$(document).keyup(function(e){ keyup_handler(e, item) });​​​​​​​​​ 

除非你以某种方式设法使文档级转化为动态元素?

相关问题