copy command using JS

…衆ロ難τιáo~ 提交于 2019-12-11 07:32:14

问题


Please help me solve this code. I've been fixing this for a month. Thank you for helping!

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

copyText('JS is love');

回答1:


  1. The .select() function call doesn't belong to strings but instead HTMLInputElement such as TextArea
  2. document.execCommand('copy') can only run as a result of an user action. In other words, it must belong inside an EventListener such as 'click'

Please refer to How do I copy to the clipboard in JavaScript? for more details



来源:https://stackoverflow.com/questions/45517613/copy-command-using-js

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!