2017-08-05 92 views
1

请帮我解决这段代码。我已经修复了一个月。感谢您的帮助!使用JS复制命令

function copyText(text) { 
 
\t text.select(); 
 
\t try { 
 
\t \t document.execCommand('copy'); 
 
\t } catch (err) { 
 
\t \t console.log('Unable to copy' + err); 
 
\t } 
 
} 
 

 
copyText('JS is love');

回答

1
  1. .select()函数调用不属于字符串,而是HTMLInputElement如文本区
  2. document.execCommand('copy')只能运行作为用户操作的结果。换句话说,必须在事件监听内属于比如'click'

请参考How do I copy to the clipboard in JavaScript?更多细节