本文共 1364 字,大约阅读时间需要 4 分钟。
vue中很多人推荐了clipboard,但是我这边的项目由于是jsp和vue混编,所以我只能考虑一种通用解决方案
所以,我在vue和jsp中都用了同一种写法
jsp中
vue中
Application No. : { {vue.ticketNo}}methods:{ //复制 copyText: function(text) { console.log(text); var textArea = document.createElement("textarea"); textArea.style.position = 'fixed'; textArea.style.top = '0'; textArea.style.left = '0'; textArea.style.width = '2em'; textArea.style.height = '2em'; textArea.style.padding = '0'; textArea.style.border = 'none'; textArea.style.outline = 'none'; textArea.style.boxShadow = 'none'; textArea.style.background = 'transparent'; textArea.value = text; document.body.appendChild(textArea); textArea.select(); try { var successful = document.execCommand('copy'); var msg = successful ? 'Successful copy to clipboard' : 'The browser does not support click-and-copy to clipboard'; alert(msg); } catch (err) { alert('The browser does not support click-and-copy to clipboard'); } document.body.removeChild(textArea); },}
完美解决,而且如果浏览器不兼容会弹出不支持的语句
追加一个解决onclick事件报错的问题,动态jsp拼接onclick事件报错
如果是纯数字不会报错,但是如果是字符串会有undifined的情况,导致onclick点不出效果
$('td:eq(1)', row).append("");
特殊说明
οnclick=\"copyText('"+data.ticketNo+"')\"
不这么写,浏览器不认,不知道为啥,希望看到的大神帮忙回复下
转载地址:http://eupmf.baihongyu.com/