Ctrl+V之外还有其他文本捕获的替代方案吗?

5

我正在尝试在以下情况下捕获Ctrl+V事件中的文本。

  1. Creating a textarea in the page and setting height 0px and width 0px. like below

      <textarea id="a" style="height:0px;width:0px"></textarea>
    
  2. On pressing V key i am setting the focus to that textarea and then using Ctrl+V button. Like below..

     shortcut.add("X",function() {
      $('#a').focus();
     });
     // In between user have to press Ctrl+V to paste the content
     shortcut.add("V",function() {
      alert($('#a').val());
     });
    
我认为这是一种极其低效的方法,期待有价值的建议来改善它...

它被称为谷歌。https://dev59.com/73VC5IYBdhLWcg3wnCf6和http://www.quirksmode.org/dom/events/tests/cutcopypaste.html - user938787
1
恐怕这方面没有真正的替代方案。 - Tim Down
1
@Consciousness:我认为你没有正确阅读问题:OP想要捕获粘贴的文本,这通常不可能使用粘贴事件。 - Tim Down
2个回答

2
您可以将事件附加到paste事件上。
$('textarea').bind('paste', function() {
   // Hello, Mr. Paste!
});

在粘贴事件中,我无法捕获已粘贴的文本。例如$(this).val()返回null/先前粘贴的文本。 - Exception
每次更改字符串时,您可以将其更新为变量,如果它是粘贴事件,则比较字符串。 - user938787

0

你可以将CTRL + V捕获为:

$(document).ready(function()
{
    var ctrlDown = false;
    var ctrlKey = 17, vKey = 86;
$(document).keydown(function(e) { if (e.keyCode == ctrlKey) ctrlDown = true; }).keyup(function(e) { if (e.keyCode == ctrlKey) ctrlDown = false; });
$("textarea").keydown(function(e) { if (ctrlDown && (e.keyCode == vKey)) return false; }); });

我有一个shortcut.js文件,可以即时捕获Ctrl+V操作。这不是我的问题。 - Exception
不是这个问题的答案,但却回答了我的问题。谢谢。https://dev59.com/K3VD5IYBdhLWcg3wGHeu#8760097 - pelms

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接