网上说的不明不白,坑了我半天,发现必须先创建input,复制选中input的值,好™坑啊,代码如下:
html
<div class="popups-box"><div class="title">微信号</div><div class="content"><img src="__MOBILE__/images/imgs/250.png" alt=""><span id="mobileText">{$info.mobile}</span><textarea id="inputCope" style="position: absolute;top: 0;left: 0;opacity: 0;z-index: -10;">这是幕后黑手</textarea></div><div class="closes" id="closes2">复制</div></div>
js
// 点击复制closes2.onclick = function () {// 微信号var mobileText = document.getElementById("mobileText");// console.log(mobileText.innerText);var input = document.getElementById("inputCope");input.value = mobileText.innerText; // 修改文本框的内容input.select(); // 选中文本document.execCommand("copy"); // 执行浏览器复制命令closes1.style.display = "none";}