简体   繁体   English

document.execCommand copy命令无效或其他解决方案?

[英]document.execCommand copy command does not work or other solution?

What I am doing is programmatically select all text from a webpage and then copy it. 我正在做的是以编程方式从网页中选择所有文本然后复制它。 The select all works with execCommand but copy doesn't. select all适用于execCommand但复制不适用。

Here is my code: 这是我的代码:

$.ajax({
    url: $('#url').val(),
    type: 'GET',
    success: function(res) {
        $('#result').html(res.responseText);
        $('#result').fadeIn('fast');


        $('#result').focus();
        $('#result').select();

        document.execCommand('selectall');

        // copy does not work ?
        document.execCommand('copy');

    }
});

Here is Example on JsBin 这是JsBin上的示例

I also tried using flash solution such as ZeroClipboard , however it seems that one has to press their flash object/button explicitly to copy text whereas I wanted to do zeroclip.setText('whatever'); 我也试过使用诸如ZeroClipboard之类的 flash解决方案,但似乎必须明确地按下他们的flash对象/按钮来复制文本,而我想做zeroclip.setText('whatever'); without user's pressing the button. 没有用户按下按钮。

Can anyone tell how to copy text programmatically? 任何人都可以告诉如何以编程方式复制文本?

The copy command used to be protected in all browsers but IE (it would not work in other browsers). 复制命令曾在所有浏览器中受到保护,但IE(在其他浏览器中不起作用)。 Requesting the user use Ctrl+C was a common workaround. 请求用户使用Ctrl + C是一种常见的解决方法。

As of Firefox 41 (September 2015), Chrome 42 (April 2015) and Opera 29 (April 2015) this is no longer the case the copy command should be available by default in most major browsers when triggered from certain trusted (user-triggered) events, such as what would be fired in response to a button click. 截至Firefox 41(2015年9月),Chrome 42(2015年4月)和Opera 29(2015年4月),当从某些受信任(用户触发)触发时,大多数主流浏览器默认情况下不再出现复制命令的情况。事件,例如响应按钮单击而触发的事件。

The compatibility table from MDN, and W3C bug offer further information. 来自MDN的兼容性表和W3C 错误提供了进一步的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM