手机html粘贴事件,详解浏览器中的粘贴事件 paste onpaste 事件

article/2025/9/24 12:06:32

在最新的H5 API里已经有了对粘贴事件的支持, 事件名为paste,     平时用的较少,但最近想做一个粘贴板管理器,于是就调研了一下这个粘贴,复制事件.

那么调研的第一步是什么那?

当然是百度了,但高手都不百度,直接控制台调试

创建一个id为editor的文本域, 为其添加一个paste事件的监听

document.querySelector('#editor').addEventListener('paste', function(even) {

console.log(even)

})

控制台打印出来是这样的

处理函数的参数是even,even的数据结构如上图

明眼人一眼就能看出那个数据自己有用,其他几个属性稀松平常,唯独clipboardData这个属性比较  特殊,这是一个DataTransfer类型的数据.估计我们需要的粘贴对象就存储在这个clipboardData这个属性里面

果不其然,查阅资料后我得知,

DataTransfer这个数据类型的相关资料,尽管第一次接触这个类型,但还是很快能找到自己想要的,所以,知识的搜索能力对于一个人的成长必不可少.

如下:

The DataTransfer object is used to hold the data that is being dragged during a drag and drop operation. It may hold one or more data items, each of one or more data types. For more information about drag and drop, see HTML Drag and Drop API.

This object is available from the dataTransfer property of all drag events.

Properties

DataTransfer.dropEffect

Gets the type of drag-and-drop operation currently selected or sets the operation to a new type. The value must be none, copy, link or move.

DataTransfer.effectAllowed

Provides all of the types of operations that are possible. Must be one of none, copy, copyLink, copyMove, link, linkMove, move, all or uninitialized.

DataTransfer.files

Contains a list of all the local files available on the data transfer. If the drag operation doesn't involve dragging files, this property is an empty list.

DataTransfer.items Read only

Gives a DataTransferItemList object which is a list of all of the drag data.

DataTransfer.types Read only

An array of strings giving the formats that were set in the dragstart event.

Methods

Standard methods

DataTransfer.clearData()

Remove the data associated with a given type. The type argument is optional. If the type is empty or not specified, the data associated with all types is removed. If data for the specified type does not exist, or the data transfer contains no data, this method will have no effect.

DataTransfer.getData()

Retrieves the data for a given type, or an empty string if data for that type does not exist or the data transfer contains no data.

DataTransfer.setData()

Set the data for a given type. If data for the type does not exist, it is added at the end, such that the last item in the types list will be the new format. If data for the type already exists, the existing data is replaced in the same position.

DataTransfer.setDragImage()

Set the image to be used for dragging if a custom one is desired.

使用getData()方法我们可以获取自己想要的粘贴元数据,如果粘贴对象是文本,html的话可以这样获取参数,但对于文件,比如一个截图后,那么这是就要使用even.clipboardData.files 来获取了,这里获取的是一个文件列表. 可以直接上传到服务器,不过要想在Chrome中预览你的截图,那就需要使用另外一个类了,FileReader

根据这个DataTransfer类的解释我们不难得出,这个东西主要是用于drop  drag两个事件里多用

以下这个是文本粘贴的数据 可以使用even.clipboardData.getDate('text/plain') 获取粘贴的数据

完整代码.复制截图到富文本编译域里

html>

富文本编辑器

      AAffA0nNPuCLAAAAAElFTkSuQmCC

document.querySelector('#editor').addEventListener('paste', function(even,value){

console.log(even.clipboardData.files)

let file = even.clipboardData.files[0]

let fileReader = new FileReader();  // 文件解读器

fileReader.onloadend = function() {

document.querySelector('#myimg').src = fileReader.result // 将读取后的base64

}

fileReader.onerror = function(err) {

console.log(err)

}

fileReader.readAsDataURL(file) // 读取一个文件返回base64地址

})


http://chatgpt.dhexx.cn/article/3M4JhSjA.shtml

相关文章

onkeyup事件,onpaste事件,完成实时表单验证

先上效果图: input框,用户输入值的方式就两种,一种是键盘输入,一种是鼠标粘贴。onkeyup事件可以在键盘输入的时候绑定事件,onpaste"return false",设置不允许粘贴。 onkeyup进行表单验证的优…

利用JavaScript从剪贴板获取图片并上传服务器

1、参考: 使用 JavaScript File API 实现文件上传FileReader.readAsDataURL()基于Servlet3的文件上传中的示例2:基于JQuery的单文件上传 2、onpaste 剪贴板事件 onpaste 事件在用户向元素中粘贴文本时触发。有三种方式可以在元素中粘贴内容&#xff1…

Internet协议的安全性

网际层 安全问题 1.IP数据报在传递过程中被窃取 措施:读ip数据报进行加密 2.数据净荷可能被截取后修改 措施:对ip数据净荷进行完整性检测 3. 攻击者发送伪造源地址数据包欺骗接收者,也称为ip欺骗 措施:对数据包的源地址鉴别 4. A…

winget InternetOpenUrl() failed. 0x80072f7d

今天使用 winget 安装东西的时候提示: 尝试更新源时失败: winget 执行此命令时发生意外错误: InternetOpenUrl() failed. 0x80072f7d : unknown error对于我而言是因为其他原因关闭了 tls 1.2,在控制面板->网络和 Internet-&g…

Internet网际协议 --- IP地址

IP地址 一.IP地址简介二.分类IP地址三.IP子网划分与子网掩码四.无类域间路由CIDR五.路由聚合六.IP包转发 一.IP地址简介 1.IP地址,网络上的每一台主机(或路由器)的每一个接口都会分配一个全球唯一的32位的标识符。 2.将IP地址划分为固定的类…

HTTP-web-Internet

互联网(Internet) 互联网:将全球计算机互联在一起的网络。目前最流行的实现协议是TCP/IP。 万维网(WWW) 万维网:基于HTTP协议实现全球性、动态交互、跨平台的分布式图形信息系统。是互联网上应用最为广泛的…

internet协议

1:引言 IP时TCP/IP中的核心协议IP提供了一种尽力而为,无连接的数据报交付服务 2:IP头部 正常的IPV4头部大小为20字节。除非存在选项(少见)IPV6的长度是40字节,没有选项字段,IPV6有扩展头部 …

Internet网际协议---IPv4协议

IPv4协议 一.IPv4协议简介二.IPv4数据报格式(由首部和数据组成)三.数据报分片与重组四.IP协议功能及报头字段总结 一.IPv4协议简介 1.IPv4协议:网际协议版本4,一种无连接的协议,是互联网的核心,也是使用最…

网络Internet

前言 加油 原文 网络常用会话 ❶ This online store discounted all computers for sale. 该网店削价出售全部待售的电脑。 ❷ The latest laptop is very portable. 最新的这款笔记本很便携。 ❸ Add up the figures in this column. 把这一栏的数字合计一下。 ❹ Consu…

Internet协议——IP

IP Internet协议——IPIP协议与tcp/IP的关系IP地址的表示IP地址的特点地址转换——ARP反向地址转换——RARP计算子网掩码 IP数据报的格式IP数据报首部的固定部分 Internet协议——IP IP:通信子网的最高层。提供无连接的数据报传输机制。IP协议是点到点,…

InternetOpen InternetOpenUrl InternetReadFile 和 InternetCloseHandle

文章目录 InternetOpen函数功能函数声明第一个参数第二个参数第三个参数第四个参数第五个参数返回值补充:InternetOpenUrl函数功能函数声明第一个参数第二个参数第三个参数第四个参数第五个参数第六个参数返回值补充补充InternetReadFile函数功能函数声明第一个参数…

juniper设备日常操作指南

1、日常show操作 # show 查看所有配置 # show | display set 查看set格式的所有配置 # show system | display set 查看set格式的system层级配置 # show system login | display set 查看set格式的system层级下的login层级配置# run show version # run show route 1.1.1.…

这几个juniper巡检命令超实用

1. CPU利用率核查 show chassis routing-engine2. MEM利用率核查 show chassis routing-engine3. OSPF邻居关系核查 show ospf neighbor4. LDP端口状态检查 show ldp interface5. ISIS邻居关系检查 show isis adjacency6. BGP邻居关系检查 show bgp neighbor7. HSRP信息检…

Juniper

Juniper Networks[编辑] Juniper Networks公司类型 上市(NYSE:JNPR)成立1996年2月代表人物执行长:Shaygan Kheradpir 技术长:Pradeep Sindhu 董事长:Scott Kriens总部地点 加州森尼韦尔(Sunnyva…

Juniper入门之RIP

拓扑 配置 JUNOS-1 #配置接口 interfaces {em2 {unit 10 {family inet { address 12.1.1.1/24;}}}lo0 {unit 10 {family inet {address 1.1.1.1/32;}}} } protocols {rip {import import-policy;group rip-group {export export-policy;neighbor em2.10 {send …

Juniper Radius And Tacacs Server 认证测试

1. 简述 Juniper产品支持Radius、Tacacs及本地Password认证。根据不同的用户需求,3A服务器认证可能会结合域用户、LDAP、RSA-Token等认证服务器进行综合认证。此测试报告使用Juniper VSRX和Cisco ACS5.2验证3A相关认证选项。 2. 测试拓扑 使用Vmware workstation 9…

Juniper-SRX-基于域控认证的用户防火墙

目录 1:架构与环境说明 2:防火墙基础配置 3:Win-server部署 4:SRX-加域流量放行 5:Client相关域设置 6:Win-server联动SRX的相关设置 7:SRX-外部Server配置 8:SRX域控认证策略…

Juniper考试认证简单介绍

简单的对考试的介绍&说明: JUNIPER认证考试是分产品的,目前最完善的是M/T骨干路由器产品的考试; JNCIS,这个是笔试; JNCIP,实验室考试[1天]; JNCIE,实验室考试[1天];…

Juniper认证介绍

转自:http://tech.ddvip.com/2008-06/121260544745173.html Juniper网络公司技术认证项目(JNTCP)由特定平台的多级系列课程组成。每套课程都允许学员通过笔试、实际配置及故障排除考试相结合的方式来证明自己在Juniper网络公司技术方面的能力。通过考试的考生可以证…

Juniper防火墙之图解用户认证

今天正好学习到Juniper防火墙中的用户认证,那么今天就带大家来看看Juniper防火墙的用户认证。 Juniper防火墙的用户分类: 1、Admin User:管理员用户 2、Auth User:认证用户 3、IKE User:IKE第一阶段用户的认证 4、XAut…