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

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

Invoke a function after right click paste in jQuery

javascriptjquery

提问by FrankD

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 theright 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. Currently I use .keyup()event, because I need to show the remaining characters count while typing in that input field.

.change()事件也无济于事。目前我使用.keyup()事件,因为我需要在输入字段中输入时显示剩余的字符数。

回答by Jaime Torres

Kind of a hack, but:

有点像黑客,但是:

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

回答by JChen___

Why not use the "input" event?

为什么不使用“输入”事件?

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

回答by Ilia Rostovtsev

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);

回答by jbabey

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 linesfor source of the getSelectionfunction.

请参阅IE 的 document.selection.createRange 不包括getSelection函数源的前导或尾随空行