h5用js实现点击复制文本(兼容微信浏览器)

// 检测是否iOS端
function iosAgent() {
    return navigator.userAgent.match(/(iPhone|iPod|iPad);?/i);
}

// 复制文本函数,微信端,需要在用户触发 Click 事件里面才能执行成功
function copy(message) {
    if (iosAgent()) {
        console.log("input 复制方式 " + message);
        let inputObj = document.createElement("input");
        inputObj.value = message;
        document.body.appendChild(inputObj);
        inputObj.select();
        inputObj.setSelectionRange(0, inputObj.value.length);
        _execCommand('Copy');
        document.body.removeChild(inputObj);
    } else {
        console.log("document 复制方式 " + message);
        let domObj = document.createElement("span");
        domObj.innerHTML = message;
        document.body.appendChild(domObj);
        let selection = window.getSelection();
        let range = document.createRange();
        range.selectNodeContents(domObj);
        selection.removeAllRanges();
        selection.addRange(range);
        _execCommand('Copy');
        document.body.removeChild(domObj);
    }
}

// 执行浏览器命令 Copy 顺便输出一下日志,如果在移动端推荐写个方法展示日志或者用alert(msg)也行。
function _execCommand(action) {
    let is = document.execCommand(action);
    if (is) {
        console.log("复制成功");
    } else {
        console.log("复制失败");
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容