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. Ubuntu16.04 Using Note

    I meet lots of problems when i installed and use ubuntu 16.04.below is my using note: (my operating ...

  2. MXNet--DMLC-Core代码解读与宏

    MXNet--DMLC-Core代码解读与宏 dmlc-core是Distributed (Deep) Machine Learning Community的一个基础模块,这个模块用被应用到了mxne ...

  3. JSP入门3 Servlet

    需要继承类HttpServlet 服务器在获得请求的时候会先根据jsp页面生成一个java文件,然后使用jdk的编译器将此文件编译,最后运行得到的class文件处理用户的请求返回响应.如果再有请求访问 ...

  4. Linux入门之常用命令(10)软连接 硬链接

    在Linux系统中,内核为每一个新创建的文件分配一个Inode(索引结点),每个文件都有一个惟一的inode号.文件属性保存在索引结点里,在访问文件时,索引结点被复制到内存在,从而实现文件的快速访问. ...

  5. 关于CSDN, cnblog, iteye和51cto四个博客网站的比较与分析

      http://blog.csdn.net/pkucl1/article/details/6629819 CSDN: http://blog.csdn.net/ cnblog: http://www ...

  6. SpringMVC的一点理解

    1.MVC(Model-View-Controller) 用慕课网上的一个图来看看MVC Front Controller(前端控制器):把客户的请求分发给不同的控制器去生成业务数据,将生成的业务数据 ...

  7. HAProxy安装文档

    HAProxy安装文档 [toc][TOC] 一.环境说明 系统环境:CentOS Linux release 7.2.1511 (Core) 系统内核:3.10.0-327.el7.x86_64 软 ...

  8. java关于随机数和方法重构

    1.生成随机数 源代码 package Zuote; public class SuiJiShu { public static void main( String args[] ) { java.u ...

  9. 项目常用Javascript分享,包含常用验证和Cookie操作

    function IsEmail(str) { var r = /^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/; if (r.test(str)) ...

  10. JavaScript面向对象(OOP)

      前  言 JRedu 面向对象程序设计(简称OOP)是现在最流行的程序设计方法,这种方法有别于基于过程的程序设计方法.在写面向对象的WEB应用程序方面JavaScript是一种很好的选择.它能支持 ...