我整夜都在网上搜索,想弄清楚如何使用execCommand('copy')功能.最后,在https://developers.google.com/web/updates/2015/04/cut-and-copy-commands?hl=en上找到了一个非常好用的解决方案.然而,我的新困境是,当我按下从输入字段复制值的按钮时,它会为它添加额外的空白区域.因此,通过正常的复制/粘贴操作(Ctl + E和Ctl + V),输入值如下所示:
TESTTESTTESTTEST
但是当我按下按钮将输入值复制到剪贴板时,它看起来像这样:
测试
测试
测试
测试
如何删除execCommand('copy')添加到输入字段值的额外空格.我试过.replace("",""); 但这不起作用.我不确定如何继续.这是代码:
function copyValueToClipBoard(containerid) {
var valueToBeCopied = document.getElementById(containerid);
var range = document.createRange();
range.selectNode(valueToBeCopied);
window.getSelection().addRange(range);
try {
// Now that we've selected the anchor text, execute the copy command
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
console.log('Copy email command was ' + msg);
} catch(err) {
console.log('Oops, unable to copy');
}
// Remove the selections - NOTE: Should use
// removeRange(range) when it is supported
window.getSelection().removeAllRanges();
};
Customer Information
First Name: