如何使用JavaScript检测鼠标右键+粘贴?

Ris*_*smo 44 javascript jquery

有没有办法检测右键点击,然后在IE和Firefox上粘贴JavaScript?

更新:

我决定用Jquery来做:

$('#controlId').bind('paste', null, function() {
    // code
});
Run Code Online (Sandbox Code Playgroud)

这不完全是我所看到的(因为它在"ctrl + v"以及"右键单击+粘贴"中被触发但我可以解决它.

在Chrome,Firefox 3,IE 7和IE 6上进行了测试,它正在运行

odd*_*lve 76

我喜欢这个解决方案:

$('#txt_field').bind('input propertychange', function() {
   console.log($(this).val());
});
Run Code Online (Sandbox Code Playgroud)

  • 正如人们所知道的那样,这会在切割和粘贴时发生火灾. (2认同)
  • 我很好奇哪个绑定做了什么,但最终我只需要“输入”来捕获键盘输入、键盘粘贴和鼠标粘贴事件。 (2认同)

小智 23

$('#controlId').bind('paste', null, function(e) {
    if(!e.keyCode){
       /*
          since no key was down at the time of the event we can assume it was
          from the toolbar or right click menu, and not a ctrl+v
       */
    }
});
Run Code Online (Sandbox Code Playgroud)


epa*_*llo 8

使用IE浏览器,您可以使用onpaste

使用Mozilla,您可以查看输入和输入

elementReference.addEventListener("DOMCharacterDataModified", function(e){ foo(e);}, false);
Run Code Online (Sandbox Code Playgroud)

没有简单的解决方案.

埃里克


Mal*_*zad 5

使用setTimeout(),设置小的超时直到 .val() func 可以被填充。

$(document).on('paste blur keyup', '#controlId', function(event) {
    var element = $(event.target);
    setTimeout(function() {
        var text = $(element).val();
        // do something with text
    }, 100);
});
Run Code Online (Sandbox Code Playgroud)

来源:捕捉粘贴输入