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

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

                  相关文档推荐

                  在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
                  问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(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>
                            <tbody id='tKyx3'></tbody>

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