废话不多说,直接上代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
</head>
<body>
<div>欢迎光临 9527</div>
<button class="btn" onclick="copyThat()">Copy</button>
<script>
function copyThat() {
var text = document.getElementsByTagName('div')[0].innerText;
var eleInput = document.createElement('input');
eleInput.value = text;
document.body.appendChild(eleInput);
eleInput.style.display = 'none';
eleInput.select();
document.execCommand("Copy")
}
</script>
</body>
</html>