简体   繁体   English

为什么 document.execCommand("copy") 不起作用?

[英]Why document.execCommand("copy") doesn't work?

I have the following Page我有以下页面

  • When I click " Click Here To Copy ", it calls document.execCommand("copy") to copy some text to the clipboard and it works.当我单击“单击此处复制”时,它会调用 document.execCommand("copy") 将一些文本复制到剪贴板,并且可以正常工作。
  • However, when I hit the button " Open popup ", it opens a div in the same page (no iframe), then when clicking on " Click Here To Copy ", document.execCommand("copy") doesn't work .但是,当我点击“打开弹出窗口”按钮时,它会在同一页面(无 iframe)中打开一个 div,然后当点击“单击此处复制”时, document.execCommand("copy") 不起作用

Steps to reproduce :重现步骤 :

document.execCommand("copy") works : document.execCommand("copy") 作品

在此处输入图片说明

However if I open the popup, document.execCommand("copy") doesn't work但是,如果我打开弹出窗口, document.execCommand("copy") 不起作用

在此处输入图片说明

Does anyone know the reason for that please ?请问有人知道原因吗?

Thanks cheers,谢谢欢呼,

Here is my entire code :这是我的整个代码:

    function CopyToClipBoard(d){        
        var c=document.createElement("textarea");
        c.innerText=d;
        document.body.appendChild(c);       
        
        c.select();
        document.execCommand("copy");
        document.body.removeChild(c);
    }

   <div onclick="CopyToClipBoard('text to be copied')">Click Here To copy</div>

Here is my working example of copyTextToClipboard function.这是我的copyTextToClipboard函数的工作示例。 Please try to add textArea.focus call.请尝试添加textArea.focus调用。

    const fallbackCopyTextToClipboard = text => {
      const textArea = document.createElement('textarea');
      textArea.value = text;
      textArea.style.position = 'fixed';
      document.body.appendChild(textArea);
      textArea.focus();
      textArea.select();
      document.execCommand('copy');
      document.body.removeChild(textArea);
    }
    
    const copyTextToClipboard = text => {
      if (navigator.clipboard) {
        return navigator.clipboard.writeText(text);
      }
      return new Promise((resolve, reject) => {
        fallbackCopyTextToClipboard(text);
        resolve();
      });
    };

Here is the cause of the problem :这是问题的原因:

The following code doesn't seem to work when there is an overlay of a document.当有文档覆盖时,以下代码似乎不起作用。 For example when the caller is on a div with higher z-index or something... I'm not sure on what exactly causes this code to fail.例如,当调用者位于具有更高 z-index 或其他东西的 div 上时......我不确定究竟是什么导致此代码失败。 But it seems related to overlayers, focusable elements or something... The fact is that, when the body of the document is hidden, the created text area is unable to focus and it doesn't work.但它似乎与叠加层,可聚焦元素或其他东西有关......事实是,当文档正文被隐藏时,创建的文本区域无法聚焦并且不起作用。

function CopyToClipBoard(d){        
    var c=document.createElement("textarea");
    c.innerText=d;
    document.body.appendChild(c);       
    
    c.select();
    document.execCommand("copy");
    document.body.removeChild(c);
}

<div onclick="CopyToClipBoard('text to be copied')">Click Here To copy</div>  

Solution :解决方案 :

Instead of adding the text area to the body of the document, it can be added to the caller itself... Hence it will always be in the foreground.不是将文本区域添加到文档正文,而是可以将其添加到调用者本身...因此它将始终位于前台。 This assumes the text to copy is short such that the execution is fast enough for the user not to notice the creation and the removal of the text area...这假设要复制的文本很短,以便执行速度足够快,用户不会注意到文本区域的创建和删除......

        function CopyToClipBoard(item, d){  
                var c=document.createElement("textarea");
                c.value=d;
                c.style.maxWidth = '0px';
                c.style.maxHeight = '0px';
                item.appendChild(c);

                c.focus();        
                c.select();
                document.execCommand("copy");
                item.removeChild(c);
        }


        <div onclick="CopyToClipBoard(this,'text to be copied')">Click Here To copy</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM