如何在 JavaScript 中结合按键和点击功能?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14674456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 17:34:36  来源:igfitidea点击:

How to combine keypress & on click function in JavaScript?

javascriptjquery

提问by Sino

I have the following two functions:

我有以下两个功能:

$("input").keypress(function(event) {
   if (event.which == 13) {
   //code        
   }       
});

$('#login_submit').click(function () {        
   //code                    
});

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??

函数中使用的代码完全相同,基本上是代码重复。所以我想知道是否有办法将这些函数与 OR 语句结合起来?

回答by 0x499602D2

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);

回答by Noel Abrahams

Add a class to your HTML

将类添加到您的 HTML

<input class="myClass">
<div id="login_submit" class="myClass" ></div>

Now you can write:

现在你可以写:

$(".myClass").bind("keypress click", function(){});

Or do this:

或者这样做:

$("input").add("#login_submit").bind("keypress click", function(){});

Be aware that clicking on the input will also trigger this.

请注意,单击输入也会触发此操作。

回答by Bjoern

Why don't you do it like this?

你为什么不这样做呢?

$("input").keypress(function(event) {
    if (event.which == 13) {
        foospace.yourfunction();
    }       
});

$('#login_submit').click(function () {        
    foospace.yourfunction();                    
});

var foospace={}; 
foospace.yourfunction=function() { 
    alert("your code goes here!"); 
}

Edit:

编辑:

The callback solution by @David is slightly more elegant.

@David 的回调解决方案稍微优雅一些​​。

回答by Sebastian Viereck

I would chain the events like:

我会将事件链接起来,例如:

        var watchCurrentCursorPosition = function (){
            console.log("foo");
        }
        $("input").keypress(
            watchCurrentCursorPosition

        ).click(
            watchCurrentCursorPosition
        );

回答by Wojciech Zaj?c

For those who still are looking for an answer to the @Sino's question.

对于那些仍在寻找@Sino 问题答案的人。

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??

函数中使用的代码完全相同,基本上是代码重复。所以我想知道是否有办法将这些函数与 OR 语句结合起来?

JQuery .on()method is the way to go.

JQuery .on()方法是要走的路。

Description: Attach an event handler function for one or more eventsto the selected elements.

描述:将一个或多个事件的事件处理函数附加到所选元素。

So your code could go like this:

所以你的代码可能是这样的:

$("input").on("click keypress", function(event) {
  if (event.which === 13) {
    event.preventDefault();
    //code        
  }       
});