function copyWord(dom) { var dom='.'+dom; var copyText = $(dom).text().trim(); // 使用trim()移除两端空白 // navigator clipboard 需要https等安全上下文 if (navigator.clipboard && window.isSecureContext) { // navigator clipboard 向剪贴板写文本 return navigator.clipboard.writeText(copyText); } else { // document.execCommand('copy') 向剪贴板写文本 let input = document.createElement('input') input.style.position = 'fixed' input.style.top = '-10000px' input.style.zIndex = '-999' document.body.appendChild(input) input.value = copyText input.focus() input.select() try { let result = document.execCommand('copy') document.body.removeChild(input) if (!result || result === 'unsuccessful') { layer.msg('复制失败'); } else { layer.msg('复制成功'); } } catch (e) { document.body.removeChild(input) alert('当前浏览器不支持复制功能,请检查更新或更换其他浏览器操作') } } }
标签:调用,浏览器,dom,js,clipboard,input,navigator,document From: https://www.cnblogs.com/wxhhts/p/18316275