js点击复制txt

<button onclick="copyToClip('我是要复制的内容')"> Copy </button>

<script type="text/javascript">
function copyToClip(content) {
 var aux = document.createElement("input"); // 创建元素用于复制
 aux.setAttribute("value", content); // 内容content放入元素
 document.body.appendChild(aux); // 将元素插入页面进行调用
 aux.select();// 选择内容
 document.execCommand("copy");// 将内容复制到系统剪贴板
 document.body.removeChild(aux);// 删除创建元素
 alert("复制成功:" + aux.value);// 提示复制成功
 }
</script>

猜你喜欢

转载自blog.csdn.net/ysfb_cc/article/details/107882459