js实现复制文字到剪切板 不需要插件 兼容安卓和ios
不想加插件,然后就上网查资料了,本来已经弄好了,结果发现ios没反应,后面又加入了兼容ios的,记录一下!
<label style="float: right;color: #C6A260;cursor:pointer;" onClick="copy_order_sn()">复制</label>
//cursor:pointer; 这个也是一个坑,下面可以看看链接
var copy_order_sn = function(){
var order_sn=document.getElementById("order_sn_text").innerText;
var oInput = document.createElement('input');
oInput.value = order_sn;
document.body.appendChild(oInput);
//不影响正常布局
oInput.style.position = 'absolute';
oInput.style.top = '-50px';
oInput.style.left = '-50px';
oInput.select(); // 选择对象
var a = document.execCommand("Copy"); // 执行浏览器复制命令
if(!a){//兼容ios
oInput.select(); // 选择对象
oInput.setSelectionRange(0, oInput.value.length), document.execCommand('Copy');// 执行浏览器复制命令
}
document.body.removeChild(oInput);
var h = $(window).height();
var w = $(window).width();
$('body').append("<div id='copySuccess' style='position:absolute;z-index:9999;top:"+((h/2)-14)+"px;left:"+((w/2)-40)+"px;color:#FFFFFF;text-align: center; border-radius: 3px;background:#333333;width:80px;line-height: 27px;height:27px;font-size:13px'>复制成功</div>")
setTimeout(function(){
$('#copySuccess').remove();
},1500);
}
参考地址:https://blog.csdn.net/hzmumu/article/details/79271464
cursor:pointer; — https://blog.csdn.net/yuexiage1/article/details/51612496