javascript 如何使用“输入属性更改”事件来捕获鼠标复制和过去

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20024603/
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-10-27 17:32:06  来源:igfitidea点击:

How to use "input propertychange" events to capture just copy and past by mouse

javascripttextareajquery

提问by medBouzid

I want to capture change happened in textarea (keyup, and also copy&past), for keyup option i use :

我想捕获 textarea 中发生的更改(keyup,还有 copy&past),对于我使用的 keyup 选项:

$("textarea").keyup(function(){
   // ajax call here
});

i added this to capture pasting or cutting by mouse then trigger keyup event on the textarea:

我添加了这个来捕获鼠标粘贴或剪切,然后在 textarea 上触发 keyup 事件:

$("textarea").on('input propertychange', function() {
    $(this).trigger(keyup);
});

the problem here is if i press a key in my keyboard , i get 2 ajax calls because the second function capture also keyup event.

这里的问题是,如果我按下键盘上的一个键,我会收到 2 个 ajax 调用,因为第二个函数也捕获了 keyup 事件。

Is there a way to prevent $("textarea").on('input propertychange'...from detecting a press key ?

有没有办法防止$("textarea").on('input propertychange'...检测到按键?

采纳答案by medBouzid

after doing some research i found a solution here :

在做了一些研究之后,我在这里找到了一个解决方案:

How to run a function once when binding to multiple events that all trigger in Javascript?

绑定到所有在Javascript中触发的多个事件时如何运行一次函数?

i think this is the best solution to prevent calling event twice in my situation

我认为这是防止在我的情况下两次调用事件的最佳解决方案

回答by jacouh

Why not test this simplification? As I tested your code, without success on detecting keyup in 'input propertychange' event.

为什么不测试这种简化?当我测试您的代码时,在“输入属性更改”事件中检测到 keyup 没有成功。

You ignore keyup event:

您忽略 keyup 事件:

//$("textarea").keyup(function(){
//// ajax call here
//});

And capture only this (do ajax call with this):

并只捕获这个(用这个做ajax调用):

$("textarea").on('input propertychange', function() {
  //$(this).trigger(keyup);
  // do ajax call here
});

the latter ignores only some control keys, ie, key without corresponding character input.

后者只忽略一些控制键,即没有相应字符输入的键。