<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>点击复制内容</title>
</head>
<body>
<div οnclick="duplication(this)">点击可以复制我哦~</div>
</body>
<script>
function duplication(that){
var inp =document.createElement('input'); // create input标签
document.body.appendChild(inp) // 添加到body中--调用复制功能使用document.execCommand()方法;
inp.value =that.textContent // 给input设置value属性为需要copy的内容
inp.select(); // 选中
document.execCommand('duplication',false); // copy已经选中的内容
inp.remove(); // 删除掉这个dom--使用 inp.remove();方法
}
</script>
</html>
点击复制内容
于 2018-11-02 20:07:47 首次发布