使用jQuery复制到剪贴板(来自html标签<code>)

时间:2018-11-13 14:18:50

标签: javascript jquery

I am currently struggling with copying to clipboard via jQuery.

The code I have works if I use via on-click="Myfunction", but since I need to use it in several places, I would like to apply the function to all elements with a class. This is what I have tried.

$(".btn-clipboard").click(function(e){
  copyToClipboard($(this));
});

function copyToClipboard(element) {
  var $temp = $("<textarea>");
  $("body").append($temp);
  $temp.val($(element).siblings("code").text()).select();
  document.execCommand("copy");
  $temp.remove();
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="bd-clipboard">
  <button class="btn-clipboard" title="" data-original-title="Copy to clipboard" >Copy</button>
</div>
<figure>
  <pre>
    <code class="language-html" data-lang="html"><br>
      <span> code example that should be copied</span>
    </code>
  </pre>
</figure>

1 个答案:

答案 0 :(得分:0)

与浏览器中的某些其他操作(例如打开新窗口)一样,只能通过特定的用户操作(例如单击鼠标)将内容复制到剪贴板。例如,它不能通过计时器来完成。

这是一个代码示例:

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

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;
}
<input type="text" id="copyTarget" value="Text to Copy"> <button id="copyButton">Copy</button><br><br>
<input type="text" placeholder="Click here and press Ctrl-V to see clipboard contents">