ie是最早支持剪辑板相关事件(并且允许javascript接入)的浏览器(鼠标右键复制)

 
相关事件:
beforecopy— Fires just before the copy operation takes place.
copy— Fires when the copy operation takes place.
beforecut— Fires just before the cut operation takes place.
cut— Fires when the cut operation takes place.
beforepaste— Fires just before the paste operation takes place.
paste— Fires when the paste operation takes place.
 
因为这个标准是比较新的,所以浏览器之间差异还是有的( In Safari, Chrome, and Firefox, the beforecopy, beforecut, and beforepasteevents fire only when the context menu for the text box is displayed 
(in anticipation of a clipboard event), but Internet Explorer fires them in that case and immediately before firing the copy, cut, and pasteevents.)
 
 beforecopy, beforecut, 和 beforepaste让你有机会在发送或者读取剪辑板数据之前修改数据,但是取消这三个事件是不会取消复制,粘贴行为的,只有取消copy,cut,paste才能取消
 
剪辑板的数据可以通过clipboardData对象获取,在ie上为window属性,其他浏览器为event属性
该对象有三个方法:
getData():   获取剪辑板的数据,接收一参数(为读取数据的格式,ie为text或URL,其他浏览器接收一个MIME类型,text会识别为text/plain)
setData():  第一个参数同上为数据类型,第二个参数为需要保存的数据
clearData() :清除数据
 
兼容:
var EventUtil = {
   getClipboardText: function(event) {
       var clipboardData = (event.clipboardData || window.clipboardData);
       return clipboardData.getData(“text”);
    },
    setClipboardText: function(event, value){
       if (event.clipboardData){
              return event.clipboardData.setData(“text/plain”, value);
       } else if (window.clipboardData){
              return window.clipboardData.setData(“text”, value);
       }
    },
};
 
例子:当你某个输入框只允许数字
EventUtil.addHandler(textbox, “paste”, function(event){
event = EventUtil.getEvent(event);
var text = EventUtil.getClipboardText(event);
 
if (!/^\d*$/.test(text)){
     EventUtil.preventDefault(event);
}
});
Firefox, Safari, and Chrome 只有在paste事件才允许接入 getData()  方法
 
如果浏览器不支持的,例如opera,那么要屏蔽复制粘贴行为,就需要屏蔽复制粘贴等键盘操作和屏蔽右键弹出菜单(context menu)
 
HTML5有一个contextmenu事件,可以用来控制这个菜单怎么出现的,阻止默认菜单,用自己模仿的取代:
EventUtil.addHandler(window, “load”, function(event){
var div = document.getElementById(“myDiv”);
 
EventUtil.addHandler(div, “contextmenu”, function(event){
event = EventUtil.getEvent(event);
EventUtil.preventDefault(event);
 
var menu = document.getElementById(“myMenu”);       //自己用div做的菜单
menu.style.left = event.clientX + “px”;
menu.style.top = event.clientY + “px”;
menu.style.visibility = “visible”;
});
 
EventUtil.addHandler(document, “click”, function(event){
document.getElementById(“myMenu”).style.visibility = “hidden”;
});
});

Clipboard 剪辑板的更多相关文章

  1. 十个Flex/Air疑难杂症及解决方案简略

    十个Flex/Air疑难杂症及解决方案简略 转自http://blog.sban.us/40.html 最近去一家台企,对方给我出了十道“难道”:在TileList中如果選擇檔過多,會出現捲軸,當拖動 ...

  2. [0412]SQL Server 2008 R2 安装 & 设置

    SQL Server 2008 R2 安装 & 设置 Sql Server 安装 安装环境: Windows 10 1709 64位 安装文件: Sql Server 2008 R2 Sql ...

  3. words2

    餐具:coffee pot 咖啡壶coffee cup 咖啡杯paper towel 纸巾napkin 餐巾table cloth 桌布tea -pot 茶壶tea set 茶具tea tray 茶盘 ...

  4. .net持续集成单元测试篇之单元测试简介以及在visual studio中配置Nunit使用环境

    系列目录 单元测试及测试驱动开发简介 什么是单元测试 单元测试是一段自动化的代码,这段代码调用被测试的工作单元,之后对这个单元的单个最终结果的某些假设进行检验.单元测试几乎都是用单元测试框架编写的.单 ...

  5. XDown单文件版 下载工具 支持磁力等多种链接方式下载

    原来的程序不带剪辑板探测,不支持迅雷链接等 增加功能后优化制作单文件版本. 下载类型为下图 magnet:?xt=urn:btih:836A228D932EF1C7EA1DD99D5D80B7CB0C ...

  6. fiddler选项卡-Statistc(统计)

    Statistc Statistc是fiddler用来对session列表里的Session相关情况的统计,利用这个选项,可以对请求进行性能以及其他数据分析 1.界面 2.参数详解 建议:打开fidd ...

  7. atitit.验证码识别step2------剪贴板ClipBoard copy image图像 attilax总结

    atitit.验证码识别step2------剪贴板ClipBoard copy image图像 attilax总结 剪贴板(ClipBoard)是内存中的一块区域,是Windows内置的一个非常有用 ...

  8. 2018-9-30-win10-UWP-剪贴板-Clipboard

    原文:2018-9-30-win10-UWP-剪贴板-Clipboard title author date CreateTime categories win10 UWP 剪贴板 Clipboard ...

  9. SecureCRT issue "Could not open clipboard: Assess is denied" (无法打开粘贴板:访问被拒绝)

    I got an issue when copying some line/word (actually just select the context ) in the Linux terminal ...

随机推荐

  1. Theano学习-scan循环

    \(1.Scan\) 通用的一般形式,可用于循环 减少和映射(对维数循环)是特殊的 \(scan\) 对输入序列进行 \(scan\) 操作,每一步都能得到一个输出 \(scan\) 能看到定义函数的 ...

  2. JavaScript设计模式接口

    JavaScript中实现接口的方法有三种: 第一种,使用注释的方法实现接口 特点:(1)最简单,但是功能最弱(2)利用 interface和 implement"文字"(3)把他 ...

  3. 基于nodejs+webSocket的聊天室(实现:加入聊天室、退出聊天室、在线人数、在线列表、发送信息、接收信息)

    1  安装 socket.io模块 npm install "socket.io": "latest" 2 app.js相关 ws = require('soc ...

  4. XML编程

     XML编程 XML及其语法 XML约束之DTD XML编程(CRUD---Create Read Update Delete) XML约束之Schema XML语法: XML文档的组成: 一个X ...

  5. Java简单知识梳理

    1. Java是单根继承结构:每个类都继承于Object类 ,这也就保证了每个对象都具备某些功能 2. Java类权限关键字: public -> protected -> default ...

  6. 点击jsp页面上的超链接后怎么找到对应的servlet

    首先超链接是一个像是url一部分的东西,其实不追求深入的道理可以联想到web.xml中的一个<url-pattern>,其实它俩也的确是对应关系,然后<url-pattern> ...

  7. 搭建阿里云 centos mysql tomcat jdk

    [toc] 阿里云使用centos 登录 http://www.aliyun.com/ 点击登录 进入控制 https://home.console.aliyun.com/ 云服务器 运行中 把ip输 ...

  8. php中常用的字符串格式化函数

    ltrim():从字符串左删除空格或其他预定义字符串 rtrim():从字符串的末端开始删除空白字符串或其它预定义字符 trim():从字符串的两端删除空白字符和其他预定字符 str_pad():把字 ...

  9. centos安装openoffice服务

    第一步:yum install openoffice.org-brand openoffice.org-core openoffice.org-java-common xvfb openoffice. ...

  10. ES6的Iterator遍历器

    JS表示集合的对象主要有Array.Set.Object.Map,在以前,遍历它们需要使用2种不同的方法,而现在,JS提出了Iterator机制,可以给不同的数据结构提供统一的遍历方法,就是for…o ...