【发布时间】:2017-06-27 07:45:23
【问题描述】:
我正在使用隐藏文本区域来放置一些文本,选择它,然后使用 document.execCommand 将其复制到剪贴板。这通常有效,但当文本很大时会失败(返回 false)。在 Chrome v55 中,它似乎失败了大约 180K 个字符。
以这种方式复制的数据量是否有限制?普通的 Ctrl+C 似乎不受同样的限制。
注意:有人将此标记为可能与 Does document.execCommand('copy') have a size limitation? 重复。这可能是类似的问题,但是那个被标记为我不使用的特定框架,而且也没有得到回答。我相信我的问题更笼统,仍然具有相关性。
我附上代码供参考。
function copyTextToClipboard(text) {
var textArea = document.createElement('textarea');
textArea.style.position = 'fixed';
textArea.style.top = 0;
textArea.style.left = 0;
textArea.style.width = '2em';
textArea.style.height = '2em';
textArea.style.padding = 0;
textArea.style.border = 'none';
textArea.style.outline = 'none';
textArea.style.boxShadow = 'none';
textArea.style.background = 'transparent';
textArea.value = text;
document.body.appendChild(textArea);
textArea.select();
try {
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
console.log('Copying text command was ' + msg);
} catch (err) {
console.log('Oops, unable to copy');
}
document.body.removeChild(textArea);
}
【问题讨论】:
-
查看编辑。这个问题没有解决。
标签: javascript dom clipboard