You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constcopyDataToClipboard=(data)=>(element)=>{element.value=dataelement.select()element.focus()document.execCommand('copy')}functionexecuteOnTempElement(targetDom,callback){vareTempInput=document.createElement('textarea')eTempInput.style.width='1px'eTempInput.style.height='1px'eTempInput.style.top='0px'eTempInput.style.left='0px'eTempInput.style.position='absolute'eTempInput.style.opacity='0.0'targetDom=targetDom||documenttargetDom.appendChild(eTempInput)try{callback(eTempInput)}catch(err){console.warn(' Browser does not support document.execCommand(\'copy\') for clipboard operations')}}//sendfunctionsendToClipboard(){executeOnTempElement(ducument,copyDataToClipboard('hello world'))}
The text was updated successfully, but these errors were encountered:
核心方法是执行
document.execCommand('copy')
,不过该方法需要一个可编辑元素,所以我们要先创建一个input
或者是textarea
,然后将数据赋给可编辑元素,选中值并且设置焦点,然后执行。The text was updated successfully, but these errors were encountered: