需求:
web页面上经常会有些内容,需要调用,为方便用户使用,点击按钮后将对应内容放到粘贴板上
实现
浏览器提供了一个方法***document.execCommand(‘copy’);***
可以将对应内容放到粘贴板上
copyText(text) {
var tag = document.createElement('input');
tag.setAttribute('id', 'cp_hgz_input');
tag.value = text;
document.getElementsByTagName('body')[0].appendChild(tag);
document.getElementById('cp_hgz_input').select();
//前面所有的操作都是为了调用这个方法
document.execCommand('copy');
document.getElementById('cp_hgz_input').remove();
},
调用这个方法传入想要复制的字符串就可以到粘贴板拿到对应的内容