复制链接

// 动态创建 input 元素
var aux = document.createElement('input');
// 获得需要复制的内容
if (type) {
aux.setAttribute('value', url);
} else {
aux.setAttribute('value', url);
}
// 添加到 DOM 元素中
document.body.appendChild(aux);
// 执行选中
// 注意: 只有 input 和 textarea 可以执行 select() 方法.
aux.select();
// 获得选中的内容
var content = window.getSelection().toString();
// 执行复制命令
document.execCommand('copy');
// 将 input 元素移除
document.body.removeChild(aux);

posted @ 2018-06-06 11:55  沙滩多啦图  阅读(159)  评论(0编辑  收藏  举报