<style type="text/css">
.wrapper {position: relative;}
#input_text,#div_box {position: absolute;top: 0;left: 0;opacity: 0;z-index: -10;}
</style>
<div id='div_box'>
<p id="text">要复制的文字</p>
<textarea id="input_text"></textarea>
</div>
<button onclick="copyText()">点击复制导入失败数据</button>
<div style="clear:both;"></div>
<script type="text/javascript">
function copyText() {
var text = document.getElementById("text").innerText;
var input = document.getElementById("input_text");
input.innerHTML = text; // 修改文本框的内容
input.select(); // 选中文本
document.execCommand("copy"); // 执行浏览器复制命令
alert("复制成功");
}
</script>
注意:如果要做隐藏域的话。
复制的内容一定是input 或者textarea 文本框。
文本框 不能用display:none 或者hidden隐藏。或者宽高0 隐藏(谷歌不行)
外围盒子一定不能display:none 或者hidden隐藏 不然会导致复制不上文字
可以opacty:0 position:absolute 隐藏