<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div id="con">需要复制的文字</div>
<br>
<input type="button" name="" value="点击复制" onclick="copyToClipboard('con')">
<br>
<br>
<input type="text" name="" placeholder="粘贴到这里试试">
</body>
<script>
function copyToClipboard(elementId) {
var aux = document.createElement("input");
var content = document.getElementById(elementId).innerHTML || document.getElementById(elementId).value;
aux.setAttribute("value", content);
document.body.appendChild(aux);
aux.select();
document.execCommand("copy");
document.body.removeChild(aux);
alert("复制内容成功:" + aux.value);
}
</script>
</html>