JavaScript怎么复制页面内容

发布时间:2022-08-24 11:31:40 作者:iii
来源:亿速云 阅读:94

今天小编给大家分享一下JavaScript怎么复制页面内容的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

JavaScript怎么复制页面内容

方式一:Async Clipboard API

使用 Async Clipboard API

这种方式使用起来最简单,但兼容性不太好,而且要求比较多。

JavaScript怎么复制页面内容

示例代码:

const promise = navigator.clipboard.writeText(newClipText);

需要注意,方法的返回值是个 Promise。并且使用此方法时,页面必须处于 focus 状态,否则会报错。

方式二:Document.execCommand API

使用 Document.execCommand

此方法虽然警告被废弃,不再属于 web 标准,但历史因素较多,相信浏览器还会支持很久。

JavaScript怎么复制页面内容

复制 DOM 元素内容

<p id="content">123456</p>
<button id="copyButton">复制</button>

复制 DOM 元素的时候,需要额外使用到 selection API 和 Range API。

developer.mozilla.org/en-US/docs/…

developer.mozilla.org/en-US/docs/…

示例代码:

const copyButton = document.getElementById('copyButton');
const content = document.getElementById('content');
copyButton.addEventListener('click', function () {
  const selection = window.getSelection();
  const range = document.createRange();
  // 设置选中内容
  range.selectNodeContents(content);
  // 清空选中内容
  selection.removeAllRanges();
  // 添加选中内容
  selection.addRange(range);
  document.execCommand('copy');
});

selection 需要先清空再添加 range。

这里会有一个细节问题,点击复制按钮之后,被复制的内容处于选中状态,有些突兀。

解决方式是在复制完成之后调用 selection.removeAllRanges() 清空选中内容即可。

再考虑一种情况,用户在复制之前就选中了页面的部分内容。在复制完成之后,除了清空选中的复制内容,还需要还原用户在复制之前就选中的内容。

实现代码如下:

const copyButton = document.getElementById('copyButton');
const content = document.getElementById('content');
copyButton.addEventListener('click', function () {
  const selection = window.getSelection();
  const range = document.createRange();
  // 缓存用户选中的内容
  const currentRange =
    selection.rangeCount === 0 ? null : selection.getRangeAt(0);
  // 设置文档片段
  range.selectNodeContents(content);
  // 清空选中内容
  selection.removeAllRanges();
  // 将文档片段设置为选中内容
  selection.addRange(range);
  try {
    // 复制到剪贴板
    document.execCommand('copy');
  } catch (err) {
    // 提示复制失败
  } finally {
    selection.removeAllRanges();
    if (currentRange) {
      // 还原用户选中内容
      selection.addRange(currentRange);
    }
  }
});

先缓存用户选中的内容,复制完成之后,再还原。

复制 input 元素内容

使用 input 元素对象的 select 方法即可选中内容,无需创建 range 片段设置选中内容。

示例代码:

const copyButton = document.getElementById('copyButton');
const inputEl = document.getElementById('input');
copyButton.addEventListener('click', function () {
  const selection = window.getSelection();
  const currentRange =
    selection.rangeCount === 0 ? null : selection.getRangeAt(0);
  // 选中 input 内容
  inputEl.select();
  // 复制到剪贴板
  try {
    document.execCommand('copy');
  } catch (err) {
    // 提示复制失败
    // 。。。
  } finally {
    selection.removeAllRanges();
    if (currentRange) {
      // 还原用户选中内容
      selection.addRange(currentRange);
    }
  }
});

点击复制按钮,同样不会移除之前选中的内容。

方法三:覆写 copy 事件

w3c.github.io/clipboard-a…

引用上面链接内的一段代码作为示例:

// Overwrite what is being copied to the clipboard.
document.addEventListener('copy', function (e) {
  // e.clipboardData is initially empty, but we can set it to the
  // data that we want copied onto the clipboard.
  e.clipboardData.setData('text/plain', '西炒蛋');
  // This is necessary to prevent the current document selection from
  // being written to the clipboard.
  e.preventDefault();
});

在页面复制任何内容,粘贴输出的内容都会是 “西炒蛋”。

以上就是“JavaScript怎么复制页面内容”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。

推荐阅读:
  1. PHP - 简单获取页面内容
  2. HTML页面禁止选择、页面禁止复制、页面禁止右键

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

javascript

上一篇:three.js中正交与透视投影相机应用实例分析

下一篇:vue引入elementUi后打开页面报错Uncaught TypeError如何解决

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》