本文实例为大家分享了js+html5实现点击复制文字的按钮,供大家参考,具体内容如下
图片展示:
注意css中的样式,有些页面复制不成功就是没有添加那一句造成的。
代码块
Title*{
-webkit-user-select: auto;
}
你好,好久不见!
复制
function copyArticle(event) {
const range = document.createRange();
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);
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。