前段时间有位朋友让帮忙做一个手机h5页面长按复制的功能,先上图
安卓实现起来没问题,有时候ios会报错,无奈手边也没有iPhone手机,折腾了好几次才完成,直接上关键代码
<script src="js/jquery.ui.draggable.js" type="text/javascript"></script>
<script type="text/javascript">
var clipboard = new Clipboard("#copy");
clipboard.on('success', function(e) {var u=navigator.userAgent,app=navigator.appVersion;var isAndroid=u.indexOf('Android')>-1||u.indexOf('Linux')>-1;//android终端或者uc浏览器var isiOS=!!u.match(/\(i[^;]+;(U;)?CPU.+MacOSX/);//ios终端if(isAndroid){//$("#choose").attr('capture','camera');jAlert('淘口令复制成功!打开手机淘宝即可领取','');}else{clipboard.on('success', function(e) {jAlert('淘口令复制成功!打开手机淘宝即可领取','');});clipboard.on('error', function(e) {alert('请长按选择框进行复制!')});}});clipboard.on('error', function(e) {alert("淘口令复制失败!请重试!");
});function select(){var doc = document, text = doc.getElementById("keywords"), range, selection; if (doc.body.createTextRange) { range = document.body.createTextRange(); range.moveToElementText(text); range.select(); } else if (window.getSelection) { selection = window.getSelection(); range = document.createRange(); range.selectNodeContents(text); selection.removeAllRanges(); selection.addRange(range); }else{ alert("当前浏览器不支持点击复制功能"); }
}</script>
如果有想看源码的朋友可以留言或者自己下载:
https://download.csdn.net/download/yan88888888888888888/11149913