将数据复制到剪贴板而不选择任何文本

时间:2018-07-03 15:19:37

标签: javascript html5 cross-browser

在JavaScript中是否有任何跨平台甚至大部分为跨平台的方式将文本复制到剪贴板,而无需创建元素,将其放在页面上,然后选择文本?带有“复制到剪贴板”按钮的网站如何做?我不希望它使用输入字段,因为这样的想法是将任何内容复制到剪贴板,甚至包括可能不在元素中的内容。

4 个答案:

答案 0 :(得分:9)

我相信这些天,如果您只关心在chrome,firefox,edge和Opera的现代版本中使用此功能,则可以使用navigator.clipboard。

https://developer.mozilla.org/en-US/docs/Web/API/Clipboard

例如

var amazingText = "Hello World! How sweet the content";
navigator.clipboard.writeText(amazingText);

您对野生动物园最好的选择,例如,旧的浏览器和其他任何支持,都是要检查是否定义了navigator.clipboard并回退到效率低下的旧的create throwaway元素选择和复制,作为最后的选择。

我主要是在有相当大的数据要复制到剪贴板时使用此功能,因为我注意到select和exec方法的性能问题。

编辑*

我按照建议在剪贴板.js网站上进行了简短浏览,有一句话说“该库同时依赖于Selection API和execCommand API。”这表明它可能无法回答问题。但是,我没有查看消息来源以验证这一假设。

https://clipboardjs.com/#browser-support

答案 1 :(得分:2)

希望这就是您想要的。

document.getElementById("copyButton").addEventListener("click", function() {
    copyToClipboard(document.getElementById("txt"));
});

setInterval(function(){
document.getElementById("txt").innerHTML = "Copy Me!!! @ " + new Date().getTime();
},1000);

function copyToClipboard(elem) {
	  // create hidden text element, if it doesn't already exist
    var targetId = "_hiddenCopyText_";
    var isInput = elem.tagName === "INPUT" || elem.tagName === "TEXTAREA";
    var origSelectionStart, origSelectionEnd;
    if (isInput) {
        // can just use the original source element for the selection and copy
        target = elem;
        origSelectionStart = elem.selectionStart;
        origSelectionEnd = elem.selectionEnd;
    } else {
        // must use a temporary form element for the selection and copy
        target = document.getElementById(targetId);
        if (!target) {
            var target = document.createElement("textarea");
            target.style.position = "absolute";
            target.style.left = "-9999px";
            target.style.top = "0";
            target.id = targetId;
            document.body.appendChild(target);
        }
        target.textContent = elem.textContent;
    }
    // select the content
    var currentFocus = document.activeElement;
    target.focus();
    target.setSelectionRange(0, target.value.length);
    
    // copy the selection
    var succeed;
    try {
    	  succeed = document.execCommand("copy");
    } catch(e) {
        succeed = false;
    }
    // restore original focus
    if (currentFocus && typeof currentFocus.focus === "function") {
        currentFocus.focus();
    }
    
    if (isInput) {
        // restore prior selection
        elem.setSelectionRange(origSelectionStart, origSelectionEnd);
    } else {
        // clear temporary content
        target.textContent = "";
    }
    return succeed;
}
input {
  width: 400px;
}
<div id="txt">copy me!!!</div><br><br><button id="copyButton">Copy</button><br><br>
<input type="text" placeholder="Click here and press Ctrl-V to see clipboard contents">

答案 2 :(得分:0)

您可以尝试Clipboard.js,那里有很多示例。

答案 3 :(得分:-1)

function copy() {
  /* Get the text field */
  var copyText = document.getElementById("myInput");

  /* Select the text field */
  copyText.select();

  /* Copy the text inside the text field */
  document.execCommand("copy");

  /* Alert the copied text */
  alert("Copied the text: " + copyText.value);
}

在w3schools上查看this链接。

相关问题