<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HTML5复制文字 - 我爱模板网 5.jimth.com</title>
</head>
<body>
<span id="content">你好,好久不见!</span>
<button id="copyBT">复制</button>
<script>
function copyArticle() {
//创建一个选择范围
const range = document.createRange();
//全选span#content的内容
range.selectNode(document.getElementById('content'));
//选择范围
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> 兼容火狐谷歌的js复制文字代码
最近在做C端前端时,遇到了要点击复制身份证信息的需求,我爱模板网使用了下面的js复制文字代码(因为C端用的chrome的内核,就不考虑IE了,不过下面的代码也兼容火狐),有需要的可以拿去了:
