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; 复制文本到剪贴板

        Copy text from lt;spangt; to clipboard(从 lt;spangt; 复制文本到剪贴板)

          <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; 复制文本到剪贴板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Fetch multiple links inside foreach loop(在 foreach 循环中获取多个链接)
                  Backbone Fetch Request is OPTIONS method(Backbone Fetch Request 是 OPTIONS 方法)
                  Fetch API leaks memory in Chrome(Fetch API 在 Chrome 中泄漏内存)
                  How can I download and save a file using the Fetch API? (Node.js)(如何使用 Fetch API 下载和保存文件?(Node.js))
                  Send blob data to node using fetch, multer, express(使用 fetch、multer、express 将 blob 数据发送到节点)
                  Sending a custom User-Agent string along with my headers (fetch)(发送自定义用户代理字符串以及我的标头(获取))
                    <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>
                            <tbody id='tKyx3'></tbody>

                          • <small id='tKyx3'></small><noframes id='tKyx3'>