javascript 如何从javascript中的ctrl+v键事件获取复制值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7621500/
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
How to get copied value from ctrl+v key event in javascript
提问by Bar?? Velio?lu
This method detects ctrl + v event but I couldnt find how to get the value of it ?
此方法检测 ctrl + v 事件,但我找不到如何获取它的值?
Thanks in advance,
提前致谢,
$(".numeric").keydown(function (event) {
if (event.shiftKey) {
event.preventDefault();
}
switch (event.keyCode) {
case 86:
if (event.ctrlKey) { // detects ctrl + v
var value = $(this).val();
alert(value); // returns ""
}
break;
}
回答by Martin Jespersen
All you have to do it hook into the paste event, let it finish by breaking the callstack and then read the value.
您要做的就是将其挂接到粘贴事件中,通过破坏调用堆栈然后读取值来让它完成。
It might seem ugly but it is very crossbrowser friendly and saves you creating a lot of crappy code just to read the actual clipboard.
它可能看起来很丑,但它对跨浏览器非常友好,并且可以节省您创建大量蹩脚代码来阅读实际剪贴板。
$(".numeric").bind('paste', function (event) {
var $this = $(this); //save reference to element for use laster
setTimeout(function(){ //break the callstack to let the event finish
alert($this.val()); //read the value of the input field
},0);
});
See it in action here: http://jsfiddle.net/Yqrtb/2/
在这里查看它的实际效果:http: //jsfiddle.net/Yqrtb/2/
Update:
更新:
Since i just recently had to do something similar, i thought i'd share my final implementation, it goes like this:
由于我最近不得不做类似的事情,我想我会分享我的最终实现,它是这样的:
$('textarea').on('paste',function(e) {
//store references for lateer use
var domTextarea = this,
txt = domTextarea.value,
startPos = domTextarea.selectionStart,
endPos = domTextarea.selectionEnd,
scrollTop = domTextarea.scrollTop;
//clear textarea temporarily (user wont notice)
domTextarea.value = '';
setTimeout(function () {
//get pasted value
var pastedValue = domTextarea.value;
//do work on pastedValue here, if you need to change/validate it before applying the paste
//recreate textarea as it would be if we hadn't interupted the paste operation
domTextarea.value = txt.substring(0, startPos) + pastedValue + txt.substring(endPos, txt.length);
domTextarea.focus();
domTextarea.selectionStart = domTextarea.selectionEnd = startPos + pastedValue.length;
domTextarea.scrollTop = scrollTop;
//do work on pastedValue here if you simply need to parse its ccontents without modifying it
}, 0);
});
回答by hvgotcodes
It's very much browser dependent. The data is in the event passed to your handler. In safari/chrome, we are listening for the paste
event on the input and then doing
它在很大程度上依赖于浏览器。数据在传递给处理程序的事件中。在 safari/chrome 中,我们正在侦听paste
输入上的事件,然后执行
event.clipboardData.getData('text/plain')
event.clipboardData.getData('text/plain')
in the callback and it seems to work ok. You should use your favorite debugger and put a breakpoint into your paste event handler, and look at the event that is passed in.
在回调中,它似乎工作正常。您应该使用您最喜欢的调试器并在粘贴事件处理程序中放置一个断点,然后查看传入的事件。
回答by Tim Down
You need a hack. I've detailed one on Stack Overflow a few times before:
你需要一个黑客。我之前曾多次详细介绍过 Stack Overflow:
回答by Fer León
It's very simple, code below works fine, in my case takes text contained on clipboard.
这很简单,下面的代码工作正常,在我的例子中需要包含在剪贴板上的文本。
function onPaste(info) {
var copied = event.view.clipboardData.getData("Text");
alert(copied);
}
In this case variable called copiedcontains the value that you need.
在这种情况下,称为copy 的变量包含您需要的值。
回答by Jure Majeri?
If you need a simple solution with minimal scripting for url link pasting, the solution could be looking at the length of text inserted into a textarea. The code is pretty self explanatory. Basically you look at the length of the textarea value and save the last two changing values. If the difference between the two values is bigger than 5 characters, you assume that the user pasted the text. An average user normally wouldn't press more than five keys at a time, thus removing the unnecessary assumptions.
如果您需要一个简单的解决方案,使用最少的脚本来粘贴 url 链接,则解决方案可能是查看插入文本区域的文本长度。该代码是不言自明的。基本上,您查看 textarea 值的长度并保存最后两个更改的值。如果两个值之间的差值大于 5 个字符,则假定用户粘贴了文本。普通用户一次通常不会按下超过五个键,从而消除了不必要的假设。
This solution only works on pasted texts that are longer than 5 characters, which URLs usually are.
此解决方案仅适用于长度超过 5 个字符的粘贴文本,这通常是 URL。
var length1 = 0,
length2 = 0;
$('textarea').keyup(function (e) {
//save last two inserted values
length2 = length1;
length1 = $(this).val().length;
//check the difference and get the substring
if (length1 - length2 > 5) {
alert('Probable Ctrl + V. Pasted text: ' + $(this).val().substring(length1, length2));
}
});