用HBuilder和MUI實現複製連結到手機剪下板
阿新 • • 發佈:2018-12-04
html程式碼如下:
<input type="text" name="" id="link" value="https://blog.csdn.net/jiaonizuoren"/>
<div class="mui-btn mui-btn-primary" type="button" onclick="copyShareUrl()"/>複製連結</div>
js程式碼如下:
// 實現一鍵複製連結到手機剪下板的功能 function copyShareUrl(){ mui.plusReady(function(){ var copy_content = document.getElementById("link").value; console.log(copy_content); //判斷裝置是android還是ios if(mui.os.ios){ //ios var UIPasteboard = plus.ios.importClass("UIPasteboard"); var generalPasteboard = UIPasteboard.generalPasteboard(); //設定/獲取文字內容: generalPasteboard.plusCallMethod({ setValue:copy_content, forPasteboardType: "public.utf8-plain-text" }); generalPasteboard.plusCallMethod({ valueForPasteboardType: "public.utf8-plain-text" }); mui.toast("複製連結成功"); //自動消失提示框 }else{ //android var context = plus.android.importClass("android.content.Context"); var main = plus.android.runtimeMainActivity(); var clip = main.getSystemService(context.CLIPBOARD_SERVICE); plus.android.invoke(clip,"setText",copy_content); mui.toast("複製連結成功"); //自動消失提示框 } }); }
效果如圖: