js Copy剪切板


第一种 clipboardjs

https://clipboardjs.com/

需要以来demo元素,自动添加事件,

我想要的是直接setText(txt)这种,在方法内直接执行copy文本

第二种 js 网站上找到的

jS原生,火狐,chrome和IE都支持的复制剪切板功能window.clipboardData.setData

 

function copyToClipboard(txt) { 
    if (window.clipboardData) { 
        window.clipboardData.clearData(); 
        window.clipboardData.setData("Text", txt); 
        alert("<strong>复制</strong>成功!") 
    } else if (navigator.userAgent.indexOf("Opera") != -1) { 
        window.location = txt; 
        alert("<strong>复制</strong>成功!"); 
    } else if (window.netscape) { 
        try { 
            netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); 
        } catch (e) { 
            alert("被浏览器拒绝!\n请在浏览器地址栏输入'about:config'并回车\n然后将 'signed.applets.codebase_principal_support'设置为'true'"); 
        } 
        var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard); 
        if (!clip) 
            return; 
        var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable); 
        if (!trans) 
            return; 
        trans.addDataFlavor('text/unicode'); 
        var str = new Object(); 
        var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString); 
        var copytext = txt; 
        str.data = copytext; 
        trans.setTransferData("text/unicode", str, copytext.length * 2); 
        var clipid = Components.interfaces.nsIClipboard; 
        if (!clip) 
            return false; 
        clip.setData(trans, null, clipid.kGlobalClipboard); 
        alert("<strong>复制</strong>成功!") 
    }else if(copy){ 
        copy(txt); 
        alert("<strong>复制</strong>成功!") 
    } 
} 

发现最后else if(copy) 中,始终copy是false,在谷歌浏览器控制台中又是正常,项目是vue项目,所以放弃这种方法

第三种,也就是最满意的,在伟大的stackoverflow上发现的

https://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

util.copyText=function(text) { 
        // debugger
        var textArea = document.createElement("textarea");
        textArea.value = text;
        document.body.appendChild(textArea);
        textArea.focus();
        textArea.select();
 
        try {
          var successful = document.execCommand('copy');
          var msg = successful ? 'successful' : 'unsuccessful';
          console.log('Fallback: Copying text command was ' + msg);
        } catch (err) {
          console.error('Fallback: Oops, unable to copy', err);
        }
 
        document.body.removeChild(textArea);
};

分析第一种clipboardjs的代码,最终也是调用document.exeCommand方法实现copy,现在正在用

 

来自 <https://note.youdao.com/md/?defaultMode=view&fileId=983E73AA42E14650883042618FE6B738>

版权声明:本文为YES开发框架网发布内容,转载请附上原文出处连接
YES开发框架
上一篇:IVIEW FORM验证 验证数字
下一篇:格式化数值,千分号
评论列表

发表评论

评论内容
昵称:
关联文章

js Copy剪切
three.js
Node.js历史下载
Electron edge.js配置
html+js上传文件
vue打包js文件添加hash
gulp处理LESS,以及js压缩
ag-grid本地js引用 下载地址
DTCMS:后台执行JS弹窗
Bootstrap 弹窗JS组件封装
深入理解js中的yield
vue typescript 使用big.js编译build会报错
Razor模板编辑高亮CodeMirror和格式化JS-Beautify
CEFSharp致性JS脚本并获取返回数据
微信 JS 接口签名校验工具 (qq.com)
electron与node.js的版本对应关系
.NET MVC加载从后台加载JS代码块
JavaScript JS高精度计算方法与库推荐,数值运算误差
TinyMCE 代码高亮 Prism.js 对 C# language-csharp 没做处理
Node.js版本不对引起的 构建失败提示throw new ERR_INVALID_CALLBACK();

联系我们
联系电话:15090125178(微信同号)
电子邮箱:garson_zhang@163.com
站长微信二维码
微信二维码