简体   繁体   English

javascript自动将文本复制到剪贴板

[英]javascript automatic copy text to clipboard

I'm trying to copy content to clipboard without user action but this doesn't work. 我正在尝试将内容复制到剪贴板而无需用户操作,但这是行不通的。 is because firefox detect the origin of the action ? 是因为Firefox检测到了动作的起源? (click() function and not user real clic) (click()函数,而不是用户真正的习惯)

https://jsfiddle.net/ukj871dc/ https://jsfiddle.net/ukj871dc/

<div id='div'>some text</div>

$("<button id='clickme'>clickme</button>").insertAfter( "#div" );
$("<textarea id='temptext'></textarea>").insertAfter( "#clickme" );
$("#temptext").css({"position": "fixed", "bottom": "0px", "left": "0px", "opacity": "0"});

// next one doesn't work
setTimeout(function () {document.getElementById("clickme").click();}, 2000);

$("#clickme").click(function(){
var textToCopy = $('#div').text();
$('#temptext').val(textToCopy);
$('#temptext').select();
document.execCommand('copy');                                     
});

It's just for personal interest don't bother long explanation you could give me links to documentation as well 只是出于个人利益,不要长解释,您也可以给我链接到文档

If you're using Firefox pre-41, you need to enable clipboard support manually in the user.js preference file. 如果您使用的是Firefox 41之前的版本,则需要在user.js首选项文件中手动启用剪贴板支持。 Reference: https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand#Browser_compatibility 参考: https : //developer.mozilla.org/zh-CN/docs/Web/API/Document/execCommand#Browser_compatibility

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

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