可以使用以下代码实现移动端H5长按复制:
function copyText(text) {
var input = document.createElement('input');
input.setAttribute('readonly', 'readonly');
input.setAttribute('value', text);
document.body.appendChild(input);
input.setSelectionRange(0, 9999);
input.select();
if (document.execCommand('copy')) {
document.execCommand('copy');
console.log('复制成功');
}
document.body.removeChild(input);
}
在需要复制的文本内容上添加一个长按事件,调用copyText函数即可实现复制。