1. <legend id='deSXP'><style id='deSXP'><dir id='deSXP'><q id='deSXP'></q></dir></style></legend>

      <i id='deSXP'><tr id='deSXP'><dt id='deSXP'><q id='deSXP'><span id='deSXP'><b id='deSXP'><form id='deSXP'><ins id='deSXP'></ins><ul id='deSXP'></ul><sub id='deSXP'></sub></form><legend id='deSXP'></legend><bdo id='deSXP'><pre id='deSXP'><center id='deSXP'></center></pre></bdo></b><th id='deSXP'></th></span></q></dt></tr></i><div id='deSXP'><tfoot id='deSXP'></tfoot><dl id='deSXP'><fieldset id='deSXP'></fieldset></dl></div>
        • <bdo id='deSXP'></bdo><ul id='deSXP'></ul>
        <tfoot id='deSXP'></tfoot>

        <small id='deSXP'></small><noframes id='deSXP'>

      1. 从 &lt;span&gt; 复制文本到剪贴板

        时间:2023-10-02

          <tbody id='JrKXn'></tbody>
          <bdo id='JrKXn'></bdo><ul id='JrKXn'></ul>
              <legend id='JrKXn'><style id='JrKXn'><dir id='JrKXn'><q id='JrKXn'></q></dir></style></legend><tfoot id='JrKXn'></tfoot>

              <small id='JrKXn'></small><noframes id='JrKXn'>

              <i id='JrKXn'><tr id='JrKXn'><dt id='JrKXn'><q id='JrKXn'><span id='JrKXn'><b id='JrKXn'><form id='JrKXn'><ins id='JrKXn'></ins><ul id='JrKXn'></ul><sub id='JrKXn'></sub></form><legend id='JrKXn'></legend><bdo id='JrKXn'><pre id='JrKXn'><center id='JrKXn'></center></pre></bdo></b><th id='JrKXn'></th></span></q></dt></tr></i><div id='JrKXn'><tfoot id='JrKXn'></tfoot><dl id='JrKXn'><fieldset id='JrKXn'></fieldset></dl></div>

                  本文介绍了从 &lt;span&gt; 复制文本到剪贴板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我一直在尝试将 <span>innerContent 复制到我的剪贴板,但没有成功:

                  I've been trying to copy the innerContent of a <span> to my clipboard without success:

                  <span id="pwd_spn" class="password-span"></span>
                  

                  JavaScript

                  函数调用

                      document.addEventListener('DOMContentLoaded', function () {
                      document.getElementById('copy').addEventListener('click', copy_password);
                  });
                  

                  功能

                  function copy_password() {
                      var copyText = document.getElementById("pwd_spn").select();
                      document.execCommand("Copy");
                  }
                  

                  我也试过了:

                  function copy_password() {
                      var copyText = document.getElementById("pwd_spn").textContent;
                      copyText.select();
                      document.execCommand("Copy");
                  }
                  

                  似乎 .select() 不适用于 <span> 元素,因为我在这两个元素上都收到以下错误:

                  It seems like .select() doesn't work on a <span> element since I get the following error on both:

                  推荐答案

                  您可以这样做:创建一个临时文本区域并将其附加到页面,然后将 span 元素的内容添加到文本区域,从文本区域复制值并删除文本区域.

                  You could do this: create a temporary text area and append it to the page, then add the content of the span element to the text area, copy the value from the text area and remove the text area.

                  由于某些安全限制,您只能在用户与页面交互时执行Copy命令,因此您必须添加一个按钮并在用户单击按钮后复制文本.

                  Because of some security restrictions you can only execute the Copy command if the user interacted with the page, so you have to add a button and copy the text after the user clicks on the button.

                  document.getElementById("cp_btn").addEventListener("click", copy_password);
                  
                  function copy_password() {
                      var copyText = document.getElementById("pwd_spn");
                      var textArea = document.createElement("textarea");
                      textArea.value = copyText.textContent;
                      document.body.appendChild(textArea);
                      textArea.select();
                      document.execCommand("Copy");
                      textArea.remove();
                  }

                  <span id="pwd_spn" class="password-span">Test</span>
                  <button id="cp_btn">Copy</button>

                  这篇关于从 &lt;span&gt; 复制文本到剪贴板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 Google Chrome 的检查元素中复制 HTML 代码 下一篇:如何在 JavaScript 中创建文件对象的修改副本?

                  相关文章

                    <bdo id='tKyx3'></bdo><ul id='tKyx3'></ul>
                  <legend id='tKyx3'><style id='tKyx3'><dir id='tKyx3'><q id='tKyx3'></q></dir></style></legend>

                  1. <i id='tKyx3'><tr id='tKyx3'><dt id='tKyx3'><q id='tKyx3'><span id='tKyx3'><b id='tKyx3'><form id='tKyx3'><ins id='tKyx3'></ins><ul id='tKyx3'></ul><sub id='tKyx3'></sub></form><legend id='tKyx3'></legend><bdo id='tKyx3'><pre id='tKyx3'><center id='tKyx3'></center></pre></bdo></b><th id='tKyx3'></th></span></q></dt></tr></i><div id='tKyx3'><tfoot id='tKyx3'></tfoot><dl id='tKyx3'><fieldset id='tKyx3'></fieldset></dl></div>
                    <tfoot id='tKyx3'></tfoot>
                    1. <small id='tKyx3'></small><noframes id='tKyx3'>