复制内容到剪切板
程序员文章站
2022-05-16 11:22:01
...
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<p><span ondblclick="copy(this)" style="" >[email protected].bm</span></p>
</body>
<script>
function copy(that) {
that.style.background = '#efe78280';
var text = that.innerText;
var textareaEl = document.createElement('textarea');
textareaEl.setAttribute('readonly', 'readonly'); // 防止手机上弹出软键盘
textareaEl.value = text;
document.body.appendChild(textareaEl);
textareaEl.select();
var res = document.execCommand('copy');
document.body.removeChild(textareaEl);
setTimeout(function(){
alert("复制成功!");
that.style.background = '';
},800)
return res;
}
</script>
</html>