首先js脚本如下↓
<script type="text/javascript">
function copyArticle(event){
const range = document.createRange();
range.selectNode(document.getElementById('copy_p'));
const selection = window.getSelection();
if(selection.rangeCount > 0) selection.removeAllRanges();
selection.addRange(range);
document.execCommand('copy');
alert('复制成功');
}
document.getElementById('copy').addEventListener('click', copyArticle, false);
</script>
别忘了加上下面的内容!
<span id="copy_p" style="font-weight:bold;">这里是需要复制的内容!</span>
<button id="copy">点击这个按钮复制</button>
完整示例代码↓
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0,user-scalable=no" name="viewport" id="viewport">
<style type="text/css">
</style>
</head>
<body>
<span id="copy_p" style="font-weight:bold;">测试1123312</span>
<button id="copy">复制</button>
</body>
</html>
<script type="text/javascript">
function copyArticle(event){
const range = document.createRange();
range.selectNode(document.getElementById('copy_p'));
const selection = window.getSelection();
if(selection.rangeCount > 0) selection.removeAllRanges();
selection.addRange(range);
document.execCommand('copy');
alert('复制成功');
}
document.getElementById('copy').addEventListener('click', copyArticle, false);
</script>
Comments NOTHING