[英]same function for Mouse Click and Key Press
I want to call a function in jquery when mouse click or key press is occure 当鼠标单击或按键按下时,我想在jQuery中调用一个函数
one way is to write same code twice for this both event but it is not proper way 一种方法是为此两个事件编写两次相同的代码,但这不是正确的方法
can any one tell me how it is possible? 谁能告诉我这怎么可能?
write a function 写一个函数
function Process(){
//Put all your logic
}
call the function in all the event
$("some element selector").on("click keypress",function() {
Process();
});
or any other click event. 或任何其他点击事件。
Write only one function & call it on both event. 只编写一个函数并在两个事件上都调用它。
$( "#target" ).keypress(function() {
funABC(); funABC(); }); });
$( "#target" ).click(function() {
funABC(); funABC(); }); });
function funABC(){alert("DONE");}
One more shortcut : 还有一个捷径:
$( "#target" ).click(function() {
$( "#target" ).keypress();
});
$( "#target" ).keypress(function() {
funABC();
});
Use the on
method. 使用on
方法。
$("some element selector").on("click keypress",function() {
//do something
});
yes you can use like 是的,你可以像
<input type="text" />
$(document).on("click mouseenter","input",function(){});
Try this:- 尝试这个:-
$(element).on('click keypress keydown', function() { $(element).on('click keypress keydown',function(){
}); });
You also use : 您还可以使用:
$( "input" ).on('keypress mouseup',function() {
alert('done');
});
or 要么
$( "input" ).on('keypress mouseup',fun);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.