<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<button onclick="copyText()">copy</button>
<input type="text" />
<p id="text">很多文本,很多文本123</p>
<script>
function copyText() {
var copyText = document.getElementById('text').innerText;
// ie浏览器
if (window.clipboardData) {
window.clipboardData.clearData();
window.clipboardData.setData("Text", copyText);
alert("复制成功!");
} else {//非ie
var oInput = document.createElement("input");
oInput.value = copyText;
document.body.appendChild(oInput);
oInput.select();
document.execCommand("Copy");
oInput.style.display = "none";//隐藏放最后,要不不生效
}
}
</script>
</body>
</html>