let H5copyLink = () => {
if (navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)) {
/**
* ios中不能复制属性值,只能复制文本元素节点;
* (解决方案:可以把文字颜色设成背景色就能达到隐藏看不见的效果不影响显示);
*/
window.getSelection().removeAllRanges(); //这段代码必须放在前面否则无效
var Url2 = document.getElementById("copy"); //要复制文字的节点
var range = document.createRange();
// 选中需要复制的节点
range.selectNode(Url2);
// 执行选中元素
window.getSelection().addRange(range);
// 执行 copy 操作
var successful = document.execCommand("copy");
// 移除选中的元素
window.getSelection().removeAllRanges();
} else {
//创建input
let inputZ = document.createElement("input");
//添加Id,用于后续操作
inputZ.setAttribute("id", "inputCopy");
//获取当前链接
inputZ.value = con;
//创建的input添加到body
document.body.appendChild(inputZ);
//选中input中的值
document.getElementById("inputCopy").select();
//把值复制下来
try {
document.execCommand("Copy");
uni.showToast({
title: "复制成功",
duration: 3000
});
} catch (err) {
uni.showToast({
title: "此设备暂不支持复制操作,您可以长按文本内容选择复制",
duration: 2000
});
}
//删除添加的input
document.body.removeChild(inputZ);
}
return;
};
H5copyLink(con);
安卓、ios h5点击复制内容解决方案
最新推荐文章于 2024-04-16 14:54:49 发布