繁体   English   中英

鼠标单击和按键功能相同

[英]same function for Mouse Click and Key Press

当鼠标单击或按键按下时,我想在jQuery中调用一个函数

一种方法是为此两个事件编写两次相同的代码,但这不是正确的方法

谁能告诉我这怎么可能?

写一个函数

function Process(){
//Put all your logic
}

call the function in all the event 

$("some element selector").on("click keypress",function() {
     Process();
});

或任何其他点击事件。

只编写一个函数并在两个事件上都调用它。

$( "#target" ).keypress(function() {

funABC(); });

    $( "#target" ).click(function() {

funABC(); });

function funABC(){alert("DONE");}

还有一个捷径:

    $( "#target" ).click(function() {
  $( "#target" ).keypress();
});


   $( "#target" ).keypress(function() {
funABC();
});

如果要将两个处理程序注册到同一元素,则可以使用.on()来注册多个事件的处理程序

$('myelementselector').on('click keypress', function () {
    //mycode
})

使用on方法。

$("some element selector").on("click keypress",function() {
     //do something
});

是的,你可以像

<input type="text"  />


$(document).on("click mouseenter","input",function(){});

尝试这个:-

$(element).on('click keypress keydown',function(){

});

您还可以使用:

$( "input" ).on('keypress mouseup',function() {

 alert('done');

});

要么

$( "input" ).on('keypress mouseup',fun);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM