How to detect rightclick + cut/delete/paste/undo in javascript?

我只是一个虾纸丫 提交于 2019-12-12 17:30:07

问题


In my JavaScript/jQuery code, I have a text field that I run an event when the text changes using the keyup event. However currently I only account for changes done using the keyboard.

Is there a way I can detect when a text field text changed because the user did a right click and clicked on cut or delete or paste or undo?

Note: This needs to work in IE9, and preferably Firefox and chrome, but definitely needs to work in IE9.

Thanks


回答1:


jsFiddle Demo

Use jquery to bind an input event to the element like this:

$('#myInput').bind('input',function(){
  //use this for the input element when input is made
  var inputValue = this.value;//for example
 });



回答2:


As a start, this is not really the correct way to do it. But if you react on the mouseout event of a input you will most likely get it to behave the way you want.

$('#input').mouseout(function(){
  if($('#input').is(":focus"))
    console.log("Right-click");
});

Though it is to note that this might not work as well on textareas since they tend to be larger and the mouse might not be outside of it when the contextmenu has been clicked.

Note: Other than @Travis J that react to all interaction, this will (probably) only trigger an event on rightclick (and regular mouseout).



来源:https://stackoverflow.com/questions/17796731/how-to-detect-rightclick-cut-delete-paste-undo-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!