H5最完整实现一键复制功能兼容ios、安卓、PC、微信开发者工具

<div class="btnBox">
		<div class="copyTxt" style="opacity: 0; position: absolute; width: 1px; height:1px; overflow: hidden" id="copyTxt"></div>
		<div id="copy" class="btn btnRed btnSixeL" onclick="copyLink()">复制链接</div>
		<div id="backBtn" class="btn btnYes btnSixeL marTop20"  onclick="window.history.back()">返回</div>
</div>


<script type="text/javascript">
        function copyLink() {
			// 调用后端查询该id的有效链接,无效则后端生成返回
			const stockAuthId = $('input[name="radio"]:checked').val();
			if(isBlank(stockAuthId)){
				return layer.msg("请选择股东", {icon: 2},{time:1000});
			}
			$.showLoading("正在生成链接");
			// 调用后端Api查询linkUrl
			const url = pcs.common.path + 后端Api接口;
			$.post(url,{},function(data){
				if(data.code === 200){
					$.hideLoading();
					$("#copyTxt").html(data.rs);
					copy(data.rs);
				}else{
					$.hideLoading();
					return layer.msg(data.msg, {icon: 2},{time:1000});
				}
			},'json');
        }

		function copy(text){
			if(navigator.clipboard){
				navigator.clipboard.writeText(text).then(function(){
					layer.msg("复制成功");
				},function(){
					copy2(text);
				});
			}else{
				copy2(text);
			}
		}

		function copy2(text){
			if((navigator.userAgent.match(/(iPhone|iPod|iPad);?/i))){
				const range = document.createRange();
				range.selectNode(document.querySelector(".copyTxt"));
				const selection = window.getSelection();
				if(selection.rangeCount > 0) selection.removeAllRanges();
				selection.addRange(range);
				document.execCommand('Copy');
				layer.msg("复制成功");
				selection.removeAllRanges();
			}else {
				const input = document.createElement("INPUT");
				input.value = text;
				input.className = "";
				document.body.appendChild(input);
				input.select();
				if (document.execCommand('copy')) {
					document.execCommand('copy');
					layer.msg("复制成功");
				}
				document.body.removeChild(input);
			}
		}


</script>

 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值