js复制功能在html中不起作用

Min*_*ade 0 html javascript copy

我试图在我的 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>
Run Code Online (Sandbox Code Playgroud)

我该如何解决?

小智 5

这是进行复制的简单方法,请查看此更新后的代码

function copyIp()
{
    window.getSelection().selectAllChildren(document.getElementById("ip"));
    document.execCommand("Copy");
}
Run Code Online (Sandbox Code Playgroud)
<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>

    </div>
</div>
Run Code Online (Sandbox Code Playgroud)