想要实现的:
点击复制入会信息,会将绿框内所有信息拷贝,并且保持换行格式
1.页面:
需要复制的内容全部写在view内了,对其整体需要个id,
2.逻辑:
注意:document.createElement(''),如果用input,复制的内容全部在一行,如果需要换行,就用textarea。
copyText () {
let textCopy = document.createElement('textarea')
const text = document.getElementById('meetText').innerText
textCopy.value = text
document.body.appendChild(textCopy)
textCopy.select()
document.execCommand('copy')
document.body.removeChild(textCopy)
console.log('已复制')
}
3.需要复制单行的数据可以参考
copyText () {
let input = document.createElement('input')
input.setAttribute('readonly', 'readonly') // 防止手机上弹出软键盘
const meetText = document.getElementById('meetText').innerText
input.setAttribute('value', meetText)
document.body.appendChild(input)
input.select()
document.execCommand('copy')
document.body.removeChild(input)
}
4.还可以用插件npm install clipboard --save ,
由于我的环境依赖有些问题,就直接用原生的啦,具体使用可以参考https://www.cnblogs.com/zhijie1/p/14690381.html
以上目前使用是OK的,具体在使用中如有bug,我将会再更新,如有大神认为有问题,欢迎指正私聊~