本文介绍了使用JS复制命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我解决此代码.我已经解决了一个月.谢谢您的帮助!

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. .select()函数调用不属于字符串,而是HTMLInputElement,例如TextArea
  2. document.execCommand('copy')只能作为用户操作的结果运行.换句话说,它必须属于诸如'click'
  3. 之类的EventListener中
  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'

请参考如何复制到JavaScript中的剪贴板?了解更多详情

这篇关于使用JS复制命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 11:48