javascript 如何使用 jQuery 触发 Ctrl+C 键事件?

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

How to trigger Ctrl+C key event with jQuery?

javascriptjqueryclipboardjquery-events

提问by Angelus

I want to simulate Ctrl+Cto copy the text from a page. I first tried this:

我想模拟Ctrl+C从页面复制文本。我首先尝试了这个:

$('#codetext').click( function() {
  $("#codetext").trigger({
    type:  'keydown',
    which:  99
  });
}

HTML:

HTML:

<input type='text' id='codetext'>

I have also tried using $(this)instead of the selector, but the input element also has focus on it, so it doesn't run.

我也试过使用$(this)代替选择器,但输入元素也关注它,所以它不会运行。

回答by Mottie

Check out ZeroClipboard... I think it works, but I haven't tested it.

查看ZeroClipboard......我认为它有效,但我还没有测试过。

回答by Ross

not sure how to trigger ctrl+c, but there's a JQuery clipboard plugin that may be of some use:

不知道如何触发 ctrl+c,但有一个 JQuery 剪贴板插件可能会有用:

http://plugins.jquery.com/project/copy

http://plugins.jquery.com/project/copy

$("#elmID").copy() // copy all text inside #elmID.

$("#elmID").copy() // copy all text inside #elmID.

回答by Tim Down

You cannot trigger a cut, copy or paste programmatically in JavaScript (at least, not in most browsers). These actions can only come from the user. If you need to do this you'll need some kind of hack like the Flash-based things in other answers but I wouldn't even rely on them working forever.

您不能在 JavaScript 中以编程方式触发剪切、复制或粘贴(至少在大多数浏览器中不是)。这些操作只能来自用户。如果你需要这样做,你将需要一些类似其他答案中基于 Flash 的东西的 hack,但我什至不会依赖它们永远工作。

回答by Jin

Ctrl+Cevent mainly used for copy content.

Ctrl+C事件主要用于复制内容。

I tried to trigger this event on summernote.

我试图在summernote上触发这个事件。

While trying, I could know that document.execCommand('copy')can touch this problem.

在尝试时,我知道document.execCommand('copy')可以触及这个问题。

Like this you may trigger cut, pasteevent by using document.execCommand('cut')and document.execCommand('paste').

像这样,您可以使用和触发剪切粘贴事件。document.execCommand('cut')document.execCommand('paste')