移动端利用JS实现复制粘贴功能

if (navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)) {
                    /*ios复制*/
                    window.getSelection().removeAllRanges();   //将页面所有的文本区域都从选区中移除
                    var range = document.createRange();   //创建一个文本区域
                    var node = document.getElementById('#kugouCode');  //获取所需要复制的节点
                    range.selectNode(document.querySelector("#kugouCode"));   //将我们的所选节点添加到文本区域中
                    /*上面经过测试不可以range.selectNode(node)*/
                    window.getSelection().addRange(range);  //将文本区域添加至选区中
                    document.execCommand("Copy");   //执行浏览器的复制命令
                    window.getSelection().removeAllRanges();  //最后再移除选区中的所有文本区域
                    toToast.showToast('复制成功', 1000);
                }
                else {
                    /*android复制*/
                    var oInput = document.createElement('input');
                    oInput.value = $("#kugouCode").text();  //这里放后端调取的劵码值
                    document.body.appendChild(oInput);
                    oInput.select(); // 选择对象
                    document.execCommand("Copy"); // 执行浏览器复制命令
                    oInput.className = 'oInput';
                    oInput.style.display = 'none';
                    toToast.showToast('复制成功', 1000);
                }

你可能感兴趣的:(移动端利用JS实现复制粘贴功能)