/**
 * 浏览器中复制文本
 * 创建一个不可见的输入框,并将文本赋值
 * 若浏览器不绘制dom,则选取无法建立
 * @param {Object} text
 */
function copy(text) {
    const id = 'copy-hide-dom';
    let elem = document.getElementById(id);
    if (!elem) {
        elem = document.createElement('input');
        elem.style.height = '0px';
        elem.style.outline = '0px';
        elem.style.borderWidth = '0px';
        elem.style.padding = '0 0';
        elem.style.margin = '0 0';
        elem.style.position = 'fixed';
        document.body.appendChild(elem);
    }
    
    elem.value = text;
    elem.select();
    elem.setSelectionRange(0, elem.value.length);
    document.execCommand('copy');
}
最后修改:2020 年 12 月 07 日
如果觉得我的文章对你有用,请随意赞赏