简体   繁体   English

在jQuery中右键单击粘贴后调用函数

[英]Invoke a function after right click paste in jQuery

I know we can use bind paste event as below:我知道我们可以使用如下绑定粘贴事件:

$('#id').bind('paste', function(e) { 
    alert('pasting!') 
});

But the problem is, that it will call before the pasted text paste.但问题是,它会在粘贴的文本粘贴之前调用。 I want a function to be triggered after the right click -> paste text pasted on the input field, so that I can access the pasted value inside the event handler function.我希望右键单击触发一个函数 -> 粘贴粘贴在输入字段上的文本,以便我可以访问事件处理函数中的粘贴值。

.change() event also doesn't help. .change()事件也无济于事。 Currently I use .keyup() event, because I need to show the remaining characters count while typing in that input field.目前我使用.keyup()事件,因为我需要在输入字段中输入时显示剩余的字符数。

Kind of a hack, but:有点像黑客,但是:

$("#id").bind('paste', function(e) {
        var ctl = $(this);
        setTimeout(function() {
            //Do whatever you want to $(ctl) here....
        }, 100);
});

Why not use the "input" event?为什么不使用“输入”事件?

$("#id").bind('input', function(e) {
    var $this = $(this);
    console.log($this.val());
});

This will stop user from any pasting, coping or cutting with the keyboard:这将阻止用户使用键盘进行任何粘贴、处理或剪切:

$("#myField").keydown(function(event) {
   var forbiddenKeys = new Array('c', 'x', 'v');
   var keyCode = (event.keyCode) ? event.keyCode : event.which;
   var isCtrl;
   isCtrl = event.ctrlKey

     if (isCtrl) {
       for (i = 0; i < forbiddenKeys.length; i++) {
           if (forbiddenKeys[i] == String.fromCharCode(keyCode).toLowerCase()) {
             return false;
        }
     }
}
return true;
});

This one will do the same for the mouse events:这将对鼠标事件执行相同的操作:

$("#myField").bind("cut copy paste",function(event) {
   event.preventDefault();
});

Even though the above one will not prevent right clicks, the user will not be able to paste, cut or copy from that field.即使上述方法不会阻止右键单击,用户也无法从该字段粘贴、剪切或复制。

To use it after the event, like you wondered on your question, you must use JavaScript Timing Event要在事件之后使用它,就像您想知道的问题一样,您必须使用JavaScript Timing Event

setTimeout(function() {
  // your code goes here
}, 10);

I had the same issue, I opted to replicate the paste action through javascript and use that output instead:我遇到了同样的问题,我选择通过 javascript 复制粘贴操作并使用该输出:

var getPostPasteText = function (element, pastedData) {
    // get the highlighted text (if any) from the element
    var selection = getSelection(element);
    var selectionStart = selection.start;
    var selectionEnd = selection.end;

    // figure out what text is to the left and right of the highlighted text (if any)
    var oldText = $(element).val();
    var leftPiece = oldText.substr(0, selectionStart);
    var rightPiece = oldText.substr(selectionEnd, oldText.length);

    // compute what the new value of the element will be after the paste
    // note behavior of paste is to REPLACE any highlighted text
    return leftPiece + pastedData + rightPiece;
};

See IE's document.selection.createRange doesn't include leading or trailing blank lines for source of the getSelection function.请参阅IE 的 document.selection.createRange 不包括getSelection函数源的前导或尾随空行

No need to bind :无需绑定:

$(document).on('keyup input', '#myID', function () {
  //Do something      
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM