javascript - 有没有一种有效的方法可以让按钮自动将文本复制到剪贴板? (JavaScript)

标签 javascript php jquery html css

所以我已经阅读了整篇关于如何将文本复制到剪贴板的文章,但似乎没有一篇与我正在寻找的相符。 How do I copy to the clipboard in JavaScript?

该程序有几个字段,将在其中输入文本,然后将其复制并放入其他几个应用程序中。我找到了一种在 IE 中执行此操作的快速方法,但它不适用于任何其他浏览器。这是 HTML。

<SPAN ID="copytext" STYLE="height:150;width:162;background-color:pink">
This text will be copied onto the clipboard when you click the button below. Try it!
</SPAN>
<TEXTAREA ID="holdtext" STYLE="display:none;">
</TEXTAREA>
<BUTTON onClick="ClipBoard();">Copy to Clipboard</BUTTON>

然后是 JavaScript。

<SCRIPT LANGUAGE="JavaScript">

function ClipBoard() 
{
holdtext.innerText = copytext.innerText;
Copied = holdtext.createTextRange();
Copied.execCommand("Copy");
}

</SCRIPT>

该程序不能有零剪贴板或 Clippy 之类的东西,因为如果我将它放在没有这些库的另一台计算机上,它仍然需要工作。我拍过的最好照片是本文顶部发布的链接中的一张。它使用 jQuery。

var copyTextareaBtn = document.querySelector('.js-textareacopybtn');

copyTextareaBtn.addEventListener('click', function(event) {
  var copyTextarea = document.querySelector('.js-copytextarea');
  copyTextarea.select();

try {
  var successful = document.execCommand('copy');
  var msg = successful ? 'successful' : 'unsuccessful';
  console.log('Copying text command was ' + msg);
} catch (err) {
  console.log('Oops, unable to copy');}});

所以这对一个领域很有效,但我所学到的关于编程的一切都告诉我不要一遍又一遍地重复自己。特别是如果我每次重复它时只改变一件事。有一个更好的方法吗?还是 jQuery 和重复是我目前唯一的选择?

最佳答案

我最近用过这个

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

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 type="text" id="hexVal" />
<span id="copyButton">Copy</span>

关于javascript - 有没有一种有效的方法可以让按钮自动将文本复制到剪贴板? (JavaScript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38615678/

相关文章:

javascript - 移动设备的 Bootstrap 导航切换

javascript - Bootstrap Typeahead 渲染问题

javascript - 导航覆盖汉堡包菜单点击问题

php - javascript 对象到 json 字符串到 php 数组 -> POST

javascript - 如何将 footer.tpl 添加到 .php 文件

php - MySQL权限表,从PHP访问以在另一个数据库中使用?

php - 如何在 Laravel 中将图片路径保存到数据库中?

c# - JQuery Ajax 函数不调用 DNN 中的后端方法

javascript - 关于我的 Spring Rest 服务的 jQuery 和 403 错误以及 Cors 问题40

javascript - 文本框更新事件