<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<style type="text/css">
*{
-webkit-user-select: auto;
}
</style>
<body>
<span id="content">
你好,好久不见!
</span>
<input type="text" id="abc">
<button id="copyBT">复制</button>
<script>
function copyArticle(event) {
const range = document.createRange();
range.selectNode(document.getElementById('abc'));
const selection = window.getSelection();
if(selection.rangeCount > 0) selection.removeAllRanges();
selection.addRange(range);
document.execCommand('copy');
alert("复制成功!");
}
document.getElementById('copyBT').addEventListener('click', copyArticle, false);
</script>
</body>
</html>
HTML,js复制文本,输入框或div都能用
最新推荐文章于 2024-09-30 21:15:00 发布