如何组合按键和JavaScript中的点击功能? [英] How to combine keypress & on click function in JavaScript?
本文介绍了如何组合按键和JavaScript中的点击功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我具有以下两个功能:
$("input").keypress(function(event) {
if (event.which == 13) {
//code
}
});
$('#login_submit').click(function () {
//code
});
函数中使用的代码完全相同,基本上是代码复制.所以我想知道是否有一种方法可以将这些功能与OR语句组合在一起?
The code which is being used in the functions are EXACTLY the same code, basically code dublication. So i was wondering if there is a way to combine these functions with an OR statement??
推荐答案
创建您自己的回调并将其传递给事件处理程序.
Create your own callback and pass that to the event handlers.
var callback = function() {...};
$("input").keypress(function() {
if (event.which == 13) callback();
});
$('#login_submit').click(callback);
这篇关于如何组合按键和JavaScript中的点击功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文