<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>复制至剪切板DOM</title>
<script>
function myFunction(){
var oInput = document.createElement('label');
oInput.style = 'opacity: 0;position: absolute;left:-100px;top:-100px;';
oInput.innerHTML = 'hello!!!';
oInput.id = 'cpInput';
document.body.appendChild(oInput);
var range = document.createRange();
range.selectNode(document.getElementById('cpInput'));
var selection = window.getSelection();
if (selection.rangeCount > 0) {
selection.removeAllRanges();
}
selection.addRange(range);
document.execCommand('copy');
document.getElementById('cpInput').remove();
}
</script>
</head>
<body>
<p>复制至剪切板DOM</p>
<button onclick="myFunction()">点我复制</button>
<p id="demo" style="display: inline;"></p>
</body>
</html>
网友评论