【发布时间】:2018-06-15 05:21:15
【问题描述】:
我试图在我的 html 页面上获得一个复制按钮,但它不起作用 - 在 chrome 控制台中它什么也没说,它只是不会复制文本。
这是我的html:
<!doctype html>
<div class="ipDiv tk-saffran">
<div class="ipText">
<h2 id="ip">play.MineGlade.net</h2>
</div>
<div class="copyButton">
<button onclick="copyIp()">Copy IP</button>
<script>
function copyIp() {
var copyText = document.getElementById("ip");
copyText.select;
document.execCommand("Copy");
}
</script>
</div>
</div>
我该如何解决这个问题?
【问题讨论】:
-
如果您使用输入字段而不是 h2 标签,那么它会正常工作。
-
您想将“play.MineGlade.net”复制到剪贴板吗?
-
试试 var copyText = document.getElementById('ip').innerHTML; document.execCommand('复制);
标签: javascript html copy