js实现复制到剪贴板 document.createRange() API

选中元素→range→selection是一一对应的,即选区必须连续,不可以有分开的多个区域。另外,被选元素必须在dom树上,不可以是游离元素,并且实践发现display不能为none,visibility不能为hidden,即使不需要渲染。也就是说我可以创建一个游离元素,插入dom树,选择后copy,然后立刻从dom树移出,整个过程都在浏览器下一次渲染之前,所以页面上没有任何变化。

为了实现选中分开的三列table,最后的方案就是用原来的内容新建一个3列的table,按上述步骤插入body,复制后再移除。

代码:

/* 创建range对象 */
const range = document.createRange();
range.selectNode(element); // 设定range包含的节点对象
/* 窗口的selection对象,表示用户选择的文本 */
const selection = window.getSelection();
if (selection.rangeCount > 0) selection.removeAllRanges(); // 将已经包含的已选择的对象清除掉
selection.addRange(range); // 将要复制的区域的range对象添加到selection对象中
document.execCommand('copy'); // 执行copy命令,copy用户选择的文本

相关文档:

Introduction

A Range identifies a range of content in a Document, DocumentFragment or Attr. It is contiguous in the sense that it can be characterized as selecting all of the content between a pair of boundary-points.

2.3. Creating a Range

A Range is created by calling the createRange() method on the DocumentRange interface. This interface can be obtained from the object implementing the Document interface using binding-specific casting methods.

  interface DocumentRange {
Range createRange();
}

The initial state of the Range returned from this method is such that both of its boundary-points are positioned at the beginning of the corresponding Document, before any content. In other words, the container of eachboundary-point is the Document node and the offset within that node is 0.

Like some objects created using methods in the Document interface (such as Nodes and DocumentFragments), Ranges created via a particular document instance can select only content associated with that Document, or with DocumentFragments and Attrs for which that Document is the ownerDocument. Such Ranges, then, can not be used with other Document instances.

addRange

  1. Set the context object's range to range by a strong reference (not by making a copy).

A selection object represents the ranges that the user has selected. Typically, it holds only one range, As the Selection API specification notes, the Selection API was initially created by Netscape and used multiple ranges, for instance, to allow the user to select a column from a <table>. However browsers other than Gecko did not implement multiple ranges, and the specification also requires the selection to always have a single range.

Originally, the Selection interface was a Netscape feature. The original implementation was carried on into Gecko (Firefox), and the feature was later implemented independently by other browser engines. The Netscape implementation always allowed multiple ranges in a single selection, for instance so the user could select a column of a table However, multi-range selections proved to be an unpleasant corner case that web developers didn't know about and even Gecko developers rarely handled correctly. Other browser engines never implemented the feature, and clamped selections to a single range in various incompatible fashions.

This specification follows non-Gecko engines in restricting selections to at most one range, but the API was still originally designed for selections with arbitrary numbers of ranges. This explains oddities like the coexistence of removeRange() and removeAllRanges(), and agetRangeAt() method that takes an integer argument that must always be zero.

3.background-attachment 设为fixed会相对于视口静止,相当于给window设置的background,定位会以整个视口为基准(同position:fixed),不再以设置的元素为基准

4. 对checkbox来说,点击时先改变checked属性(prop里的),再触发click事件,最后触发change事件。vue对checkbox的双向绑定是在change事件里改变数据的,对有v-model的checkbox绑定click事件时要注意此时数据还没有同步(只有DOM对象变成checked了。)

checkbox里这两个事件的触发顺序,不同浏览器里可能不同

5. 伪元素的content属性可以用attr()函数,获取元素的attr属性值

document.createRange剪贴板API的更多相关文章

  1. Chrome 66 新增异步剪贴板 API

    在过去的几年里我们只能使用 document.execCommand 来操作剪贴板.不过,这种操作剪贴板的操作是同步的,并且只能读取和写入 DOM. 现在 Chrome 66 已经支持了新的 Asyn ...

  2. elasticsearch6.7 05. Document APIs(6)UPDATE API

    5. UPDATE API 更新操作可以使用脚本来更新.更新的时候会先从索引中获取文档数据(在每个分片中的集合),然后运行脚本(使用可选的脚本语言和参数),再果进行索引(还允许删除或忽略该操作).它使 ...

  3. elasticsearch6.7 05. Document APIs(2)Index API

    Single document APIs Index API Get API Delete API Update API Multi-document APIs Multi Get API Bulk ...

  4. Document flow API in SAP CRM and C4C

    Document flow API in CRM 以一个具体的例子来说明.在Appointment的Overview page上能看见一个名叫Reference的区域,这里可以维护一些其他的业务文档的 ...

  5. js复制内容到剪贴板

    我们web上的复制,有时候尽管可以用鼠标选中,然后复制,但是某些时候,文字不方便选中.因此,我们自定义一个复制按钮,然后通过点击它,把想要的内容复制到剪贴板上.我归纳总结了几种方法: 1.ZeroCl ...

  6. js实现类似微信网页版在可编辑的div中粘贴内容时过滤剪贴板的内容,光标始终在粘贴内容后面,以及将光标定位到最后的方法

    过滤剪贴板内容以及定位可编辑div光标的方法: <!DOCTYPE html><html lang="en"><head>  <meta ...

  7. 语音识别(Web Speech API)

    近期看了一个语音识别的dome-----Web Speech API 本api为js调用云端接口识别 个人测试了一下,响应速度还是比较快的 注意:本API与官网需翻墙使用和访问 展示效果: 页面代码如 ...

  8. 富文本API

    这个笔记来自网络资料的总结 简书大佬三省吾身_9862 tuobaye个人博客 富文本有相关3个API和一个新属性 var selection = window.getSelection(); var ...

  9. javascript的api设计原则

    前言 本篇博文来自一次公司内部的前端分享,从多个方面讨论了在设计接口时遵循的原则,总共包含了七个大块.系卤煮自己总结的一些经验和教训.本篇博文同时也参考了其他一些文章,相关地址会在后面贴出来.很难做到 ...

随机推荐

  1. LG3978 【[TJOI2015]概率论】

    前置:卡特兰数 记\(C_n\)为\(n\)个节点的二叉树的个数,\(C_0=1\),对于\(n \geq 1\),取一个根节点,枚举其左子树大小,有 \[C_n=\sum_{i=0}^{n-1}C_ ...

  2. 用setuptools_scm来自动控制Python包的版本

    手动管理Python包的版本号一段时间后,寻求自动化的手段,是自然而然的. 手动管理版本 手动管理Python包的版本,需要注意两个方面: 每次发布新包前,要更新包的版本号. 在安装后的默认模块下应该 ...

  3. Architecture options to run a workflow engine

    This week a customer called and asked (translated into my own words and shortened): “We do composite ...

  4. linux中和salt中的fqdn测试小节

    设置hosts文件和hostname文件 [root@dawn-hnyd-yd-1 ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdoma ...

  5. mysql之 OPTIMIZE TABLE整理碎片

    来看看手册中关于 OPTIMIZE 的描述: OPTIMIZE [LOCAL | NO_WRITE_TO_BINLOG] TABLE tbl_name [, tbl_name] ... 如果您已经删除 ...

  6. kolla-ansible部署多节点OpenStack-Pike

    一.准备工作: 系统:均为centos 7.5 这里以笔记本一台.vmware workstation做为实验环境 建议虚拟机硬盘存储为单个文件,因为在上传和使用windows镜像时文件太大容易造成虚 ...

  7. mysql 主从复制参数slave_net_timeout

    slave_net_timeout slave_net_timeout表示slave在slave_net_timeout时间之内没有收到master的任何数据(包括binlog,heartbeat), ...

  8. [2]注解(Annotation)-- 深入理解Java:注解(Annotation)自定义注解入门

    转载 http://www.cnblogs.com/peida/archive/2013/04/24/3036689.html 深入理解Java:注解(Annotation)自定义注解入门 要深入学习 ...

  9. windows 日志,IIS应用程序池回收日志

    应用程序池回收日志筛选事件ID:5074 进程被关闭:5186

  10. 简单说throw和throws的区别

    1. 区别 throws是用来声明一个方法可能抛出的所有异常信息,throws是将异常声明但是不处理,而是将异常往上传,谁调用我就交给谁处理.而throw则是指抛出的一个具体的异常类型. 2.分别介绍 ...