本文介绍了如何将变量复制到剪贴板中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将变量复制到剪贴板?

How do I copy a variable into the clipboard?

它总是返回:



代码



Code

function copy() {
    var allDesCode2 = document.getElementsByClassName("desCode2");
    var copyText = "ABC";
    for(var i=0; i<allDesCode2.length; i++) {
        copyText += allDesCode2[i].innerHTML;
    }
    copyText.select();
    document.execCommand("copy");
}


推荐答案

copy()下面的函数可以帮助从变量中复制字符串。您可以在纯JavaScript 中使用此方法,而无需任何库的帮助,例如。

The copy() function below can help to copy a string from a variable. You can use this method in pure JavaScript without the help of any libraries, such as jQuery.

function copy() {

    var copyText = "Hooray ! I will be copied";
    var el = document.createElement('textarea');
    el.value = copyText;
    el.setAttribute('readonly', '');
    el.style = {
        position: 'absolute',
        left: '-9999px'
    };
    document.body.appendChild(el);
    el.select();
    document.execCommand('copy');
    document.body.removeChild(el);
}
<button onclick="copy()">Copy</button>

这篇关于如何将变量复制到剪贴板中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 06:32
查看更多