最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to trigger Ctrl+C key event with jQuery? - Stack Overflow

programmeradmin0浏览0评论

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

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

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.

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

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

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.

Share Improve this question edited Aug 24, 2019 at 18:45 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Oct 8, 2010 at 12:57 AngelusAngelus 811 gold badge1 silver badge2 bronze badges 0
Add a comment  | 

4 Answers 4

Reset to default 4

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

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

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

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

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.

Ctrl+C event mainly used for copy content.

I tried to trigger this event on summernote.

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

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

发布评论

评论列表(0)

  1. 暂无评论