js复制span内容到剪切板

<span id="copyText">123456</span>

<span onclick="copy();">复制</span>

 

function copy() {

var text =document.getElementById("copyText").innerText;

var input = document.createElement('input'); input.setAttribute('id', 'copyInput');

input.setAttribute('value', text);

document.getElementsByTagName('body')[0].appendChild(input);

document.getElementById('copyInput').select();

document.execCommand('copy')) 

alertinfo('复制成功');

document.getElementById('copyInput').remove();

}

posted @ 2020-12-14 19:59  yuan_bao_er  阅读(992)  评论(0编辑  收藏  举报