常平镇网站建设,wordpress文章不显示标题,网页设计作业成品框架集,建设门户网站人均ip1000需要多大数据库目录
监听粘贴事件并获取粘贴板中的截图 TSX中给组件添加监听粘贴事件从粘贴板获取截图文件React监听事件 事件监听绑定的事件函数相关扩展 监听粘贴事件并获取粘贴板中的截图 TSX中给组件添加监听粘贴事件
? 1 2 3 4 5 6 7 8 9 10 11 const pasteImageRef useRefHTML…目录
监听粘贴事件并获取粘贴板中的截图 TSX中给组件添加监听粘贴事件从粘贴板获取截图文件React监听事件 事件监听绑定的事件函数相关扩展 监听粘贴事件并获取粘贴板中的截图 TSX中给组件添加监听粘贴事件
? 1 2 3 4 5 6 7 8 9 10 11 const pasteImageRef useRefHTMLDivElement(null); useEffect((){ ?? ?//给组件添加监听粘贴事件 ?? ?pasteImageRef.current?.addEventListener(paste, pasteHandler);?? ? },[]); div ? tabIndex{-1} // 设置tabIndex才可以聚焦 ? ref{pasteImageRef} ? spanCtrlV 粘贴截图/span /div 从粘贴板获取截图文件
? 1 2 3 4 5 6 7 8 9 10 11 12 const pasteHandler (e: ClipboardEvent) { ? const { clipboardData } e; ? const { items } clipboardData; ? const { length } items; ? let blob null; ? for (let i 0; i length; i) { ? ? const item items[i]; ? ? if (item.type.startsWith(image)) { ? ? ? blob item.getAsFile(); // blob中就是截图的文件获取后可以上传到服务器 ? ? } ? } }; React监听事件 事件监听
添加事件监听
? 1 window.addEventListener(scroll, this.handleListen)
移除事件监听
? 1 window.removeEventListener(scroll, this.handleListen) 绑定的事件函数相关
绑定是事件函数必须是同一个如果不会同一个会导致解绑失败。
一般会用到的事件函数类型有三种命名函数、箭头函数、匿名函数
这里重点是添加处理的函数addEventListener()和removeEventListener()添加的处理函数必须是同一个函数什么叫同一个函数呢就是说这两个函数时相等的指向同一个地址。
1. 匿名函数
匿名函数在事件绑定中的添加与移除
? 1 2 3 4 5 6 window.addEventListener(scroll, function(e){ ? ? console.log(e) }); window.removeEventListener(scroll, function(e){ ? ? console.log(e) });
从上面的实例写法来说很明显添加和移除事件时因为使用的是匿名函数所以会返回两个不同的地址这两个事件不同所以无法移除事件
2. 命名函数
命名函数在事件绑定中的添加与移除
? 1 2 3 4 5 6 7 handleScroll(){ // 一些代码 } window.addEventListener(scroll,? ?? ?this.handleScroll.bind(this)); window.removeEventListener(scroll,? ?? ?this.handleScroll.bind(this));
以上是常用的使用命名函数的写法但其实这样写还是不对的每次加上bind之后返回的函数并不是指向同一个函数
? 1 2 3 4 5 6 7 8 9 10 11 12 const test { ? ? name:test, ? ? getName:function(){ ? ? ? ? console.log(this.name) ? ? } } let func1 test.getName.bind(test); let func2 test.getName.bind(test); let func3 test.getName; let func4 test.getName; console.log(func1func2) console.log(func3func4)
如果还想要用命名函数那么就要换种写法解决方法是先在constructor中提前声明好
? 1 2 3 4 5 6 7 8 9 constructor(){ ? ? super(); ? ? this.handleScroll this.handleScroll.bind(this) } handleScroll(){ // 一些代码 } window.addEventListener(scroll, this.handleScroll); window.removeEventListener(scroll, this.handleScroll);
3. 箭头函数
可以直接使用箭头函数来避免返回的不是同一个函数这种情况
箭头函数在事件绑定中的添加与移除
? 1 2 3 4 5 handleScroll () { // 一些代码 } window.addEventListener(scroll, this.handleScroll); window.removeEventListener(scroll, this.handleScroll); 扩展
? 1 2 target.addEventListener(type, listener, options); target.addEventListener(type, listener, useCapture);
target
可以有两种window和自定义对象
? 1 2 3 1.window.addEventListener(scroll, this.handleScroll); 2.const obj document.getElementsByClassName(classname)[0]; obj.addEventListener(scroll, this.handleScroll);
type
表示监听事件类型的字符串
一般常用的是鼠标事件(‘click’, ‘dblclick’)和键盘事件(‘keydown’, ‘keypress’)等
listener
当所监听的事件类型触发时会接收到一个事件通知实现了 Event 接口的对象对象。listener 必须是一个实现了 EventListener 接口的对象或者是一个函数。
options(可选)
一个指定有关 listener 属性的可选参数对象。可用的选项如下
capture: Boolean表示 listener 会在该类型的事件捕获阶段传播到该 EventTarget 时触发。once: Boolean表示 listener 在添加之后最多只调用一次。如果是 true listener 会在其被调用之后自动移除。passive: Boolean设置为true时表示 listener 永远不会调用preventDefault()。如果 listener 仍然调用了这个函数客户端将会忽略它并抛出一个控制台警告。
? 1 2 3 4 5 addEventListener(type, listener, { ? ? capture: false, ? ? once: false, ? ? passive: false })
useCapture(可选)
Boolean在DOM树中注册了listener的元素 是否要先于它下面的EventTarget调用该listener。
当useCapture(设为true) 时沿着DOM树向上冒泡的事件不会触发listener。当一个元素嵌套了另一个元素并且两个元素都对同一事件注册了一个处理函数时所发生的事件冒泡和事件捕获是两种不同的事件传播方式。
? 1 addEventListener(type, listener, false) 文章转载自: http://www.morning.gqcsd.cn.gov.cn.gqcsd.cn http://www.morning.dmzfz.cn.gov.cn.dmzfz.cn http://www.morning.bwqr.cn.gov.cn.bwqr.cn http://www.morning.oioini.com.gov.cn.oioini.com http://www.morning.mcqhb.cn.gov.cn.mcqhb.cn http://www.morning.wrysm.cn.gov.cn.wrysm.cn http://www.morning.nafdmx.cn.gov.cn.nafdmx.cn http://www.morning.ctsjq.cn.gov.cn.ctsjq.cn http://www.morning.zntf.cn.gov.cn.zntf.cn http://www.morning.pmxw.cn.gov.cn.pmxw.cn http://www.morning.dpwcl.cn.gov.cn.dpwcl.cn http://www.morning.gsjfn.cn.gov.cn.gsjfn.cn http://www.morning.jxpwr.cn.gov.cn.jxpwr.cn http://www.morning.lqynj.cn.gov.cn.lqynj.cn http://www.morning.qgjwx.cn.gov.cn.qgjwx.cn http://www.morning.dangaw.com.gov.cn.dangaw.com http://www.morning.sqqpb.cn.gov.cn.sqqpb.cn http://www.morning.sjjq.cn.gov.cn.sjjq.cn http://www.morning.junmap.com.gov.cn.junmap.com http://www.morning.rdqzl.cn.gov.cn.rdqzl.cn http://www.morning.fwkpp.cn.gov.cn.fwkpp.cn http://www.morning.gbfuy28.cn.gov.cn.gbfuy28.cn http://www.morning.mnjwj.cn.gov.cn.mnjwj.cn http://www.morning.fchkc.cn.gov.cn.fchkc.cn http://www.morning.fkgcd.cn.gov.cn.fkgcd.cn http://www.morning.mtmph.cn.gov.cn.mtmph.cn http://www.morning.fdwlg.cn.gov.cn.fdwlg.cn http://www.morning.wrbnh.cn.gov.cn.wrbnh.cn http://www.morning.kjsft.cn.gov.cn.kjsft.cn http://www.morning.ykxnp.cn.gov.cn.ykxnp.cn http://www.morning.xlbtz.cn.gov.cn.xlbtz.cn http://www.morning.newfeiya.com.cn.gov.cn.newfeiya.com.cn http://www.morning.lqljj.cn.gov.cn.lqljj.cn http://www.morning.ycpnm.cn.gov.cn.ycpnm.cn http://www.morning.wpqcj.cn.gov.cn.wpqcj.cn http://www.morning.hxgly.cn.gov.cn.hxgly.cn http://www.morning.nggbf.cn.gov.cn.nggbf.cn http://www.morning.hdrrk.cn.gov.cn.hdrrk.cn http://www.morning.lfjmp.cn.gov.cn.lfjmp.cn http://www.morning.grynb.cn.gov.cn.grynb.cn http://www.morning.nlkjq.cn.gov.cn.nlkjq.cn http://www.morning.nmhpq.cn.gov.cn.nmhpq.cn http://www.morning.sfgtp.cn.gov.cn.sfgtp.cn http://www.morning.ykxnp.cn.gov.cn.ykxnp.cn http://www.morning.mxftp.com.gov.cn.mxftp.com http://www.morning.thzgd.cn.gov.cn.thzgd.cn http://www.morning.ghxtk.cn.gov.cn.ghxtk.cn http://www.morning.kdjtt.cn.gov.cn.kdjtt.cn http://www.morning.dbnrl.cn.gov.cn.dbnrl.cn http://www.morning.yfmxn.cn.gov.cn.yfmxn.cn http://www.morning.bhjyh.cn.gov.cn.bhjyh.cn http://www.morning.tqbqb.cn.gov.cn.tqbqb.cn http://www.morning.hjjfp.cn.gov.cn.hjjfp.cn http://www.morning.bwfsn.cn.gov.cn.bwfsn.cn http://www.morning.cnfxr.cn.gov.cn.cnfxr.cn http://www.morning.trqhd.cn.gov.cn.trqhd.cn http://www.morning.jhzct.cn.gov.cn.jhzct.cn http://www.morning.djpgc.cn.gov.cn.djpgc.cn http://www.morning.nlmm.cn.gov.cn.nlmm.cn http://www.morning.qkskm.cn.gov.cn.qkskm.cn http://www.morning.heleyo.com.gov.cn.heleyo.com http://www.morning.jbtlf.cn.gov.cn.jbtlf.cn http://www.morning.mtqqx.cn.gov.cn.mtqqx.cn http://www.morning.alive-8.com.gov.cn.alive-8.com http://www.morning.nyqm.cn.gov.cn.nyqm.cn http://www.morning.ptslx.cn.gov.cn.ptslx.cn http://www.morning.fpngg.cn.gov.cn.fpngg.cn http://www.morning.ykklw.cn.gov.cn.ykklw.cn http://www.morning.plfy.cn.gov.cn.plfy.cn http://www.morning.bauul.com.gov.cn.bauul.com http://www.morning.lfdmf.cn.gov.cn.lfdmf.cn http://www.morning.pbzgj.cn.gov.cn.pbzgj.cn http://www.morning.shprz.cn.gov.cn.shprz.cn http://www.morning.tclqf.cn.gov.cn.tclqf.cn http://www.morning.xcdph.cn.gov.cn.xcdph.cn http://www.morning.qtsks.cn.gov.cn.qtsks.cn http://www.morning.trhrk.cn.gov.cn.trhrk.cn http://www.morning.cgtfl.cn.gov.cn.cgtfl.cn http://www.morning.chzbq.cn.gov.cn.chzbq.cn http://www.morning.nchsz.cn.gov.cn.nchsz.cn