关于对div,span,pre等标签里的内容实现一键复制功能
直接上代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>做个笔记</title>
<script src="../js/jquery-3.3.1.min.js.js"></script>
</head>
<body>
<span id="biao1">不喜勿喷</span>
<input type="button" value="复制" onclick="selectText(document.getElementById('biao1'));"/>
<script>
function selectText(x) {
if (document.selection) {
var range = document.body.createTextRange();//ie
range.moveToElementText(x);
range.select();
} else if (window.getSelection) {
var selection = window.getSelection();
var range = document.createRange();
selection.removeAllRanges();
range.selectNodeContents(x);
selection.addRange(range);
}
document.execCommand("copy");
}
</script>
</body>
</html>
结果: