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

时间:2009-01-14 01:51:52

标签: javascript jquery

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

更新

我决定使用Jquery来做这件事:

$('#controlId').bind('paste', null, function() {
    // code
});

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

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

8 个答案:

答案 0 :(得分:73)

我喜欢这个解决方案:

$('#txt_field').bind('input propertychange', function() {
   console.log($(this).val());
});

答案 1 :(得分: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
       */
    }
});

答案 2 :(得分:8)

使用IE,你有onpaste

使用Mozilla,您可以查看 oninput 和

elementReference.addEventListener("DOMCharacterDataModified", function(e){ foo(e);}, false);

没有简单的解决方案。

埃里克

答案 3 :(得分:4)

使用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);
});

来源:Catch paste input

答案 4 :(得分:0)

我在IE8中遇到了同样的问题。 Chrome允许我识别右键单击粘贴,但IE8不是。

我能够使用Aaron所描述的鼠标离开功能解决JQUERY的问题,但这里是代码:

for IE8:
    $( "#field" ).mouseleave(function() {
                doStuff());
            });

for Chrome:
    $('#field').bind('input',function() {
                doStuff();
            });

答案 5 :(得分:0)

我做了以下只会在mouseup上触发:

onmouseup="jQuery(this).on('paste',function(event){setTimeout(function(){alert('Paste detected!');},100);});"

答案 6 :(得分:0)

如果您正在使用vue,则可以发出并传递粘贴的值,如下所示:

 <v-text-field 
   @input="$emit('rightclickpaste',$event)"  
 >             
 </v-text-field>

答案 7 :(得分:-1)

你可以尝试的廉价黑客(有效):

  • jQuery的mouseleave功能。

我注意到IE8,如果右键单击文本框然后选择“粘贴”,它会延迟“mouseleave”事件,直到粘贴完成。所以它在粘贴后一直闪现! :)适合我,让我完全摆脱困境。

这仅适用于Intranet应用程序,我尚未在Firefox等中进行测试。

干杯

相关问题