首页 > 代码库 > 深入理解DOM事件类型系列第四篇——剪贴板事件

深入理解DOM事件类型系列第四篇——剪贴板事件

×
目录
[1]定义 [2]对象方法 [3]应用

前面的话

  剪贴板操作可能看起来不起眼,但是却十分有用,可以增强用户体验,方便用户操作。本文将详细介绍剪贴板事件

 

定义

  剪贴板操作包括剪切(cut)、复制(copy)和粘贴(paste)这三个操作,快捷键分别是ctrl+x、ctrl+c、ctrl+v。当然也可以使用鼠标右键菜单进行操作

  关于这3个操作共对应下列6个剪贴板事件

  copy:在发生复制操作时触发

  cut:在发生剪切操作时触发

  paste:在发生粘贴操作时触发

  IE浏览器只有在文本中选定字符时,copy和cut事件才会发生。且在非文本框中(如div元素)只能发生copy事件

  firfox浏览器只有焦点在文本框中才会发生paste事件

<input value="text" id="test"><script>test.onpaste= test.oncopy = test.oncut = function(e){    e = e || event;    test.value = e.type;    return false;}</script>

<iframe style="width: 100%; height: 40px;" src="http://sandbox.runjs.cn/show/xukar5yk" frameborder="0" width="320" height="240"></iframe>

  beforecopy:在发生复制操作前触发

  beforecut:在发生剪切操作前触发

  beforepaste:在发生粘贴操作前触发

<input value="text" id="test"><script>test.onbeforepaste= test.onbeforecopy = test.onbeforecut = function(e){    e = e || event;    test.value = e.type;    return false;}</script>

<iframe style="width: 100%; height: 40px;" src="http://sandbox.runjs.cn/show/6bmglsnj" frameborder="0" width="320" height="240"></iframe>

对象方法

  剪贴板中的数据存储在clipboardData对象中。对于IE浏览器来说,这个对象是window对象的属性;对于其他浏览器来说,这个对象是事件对象的属性

    e = e || event;    var clipboardData = http://www.mamicode.com/e.clipboardData || window.clipboardData;

  这个对象有三个方法:getData()、setData()和clearData ()

getData()

  getData()方法用于从剪贴板中取得数据,它接受一个参数,即要取得的数据的格式。在IE中,有两种数据格式:"text" 和 "URL"。在其他浏览器中,这个参数是一种MIME类型;不过,可以用"text"代表

  [注意]在IE浏览器中,cut和copy事件中的getData()方法始终返回null;而其他浏览器始终返回空字符串‘‘。但如果和setDada()方法配合,就可以正常使用

<input id="test" value="http://www.mamicode.com/123"><script>test.onpaste=function(e){    e = e || event;    var clipboardData = http://www.mamicode.com/e.clipboardData || window.clipboardData;    test.value = ‘测试‘ + clipboardData.getData(‘text‘);    return false;}</script>

<iframe style="width: 100%; height: 40px;" src="http://sandbox.runjs.cn/show/4z3ldi01" frameborder="0" width="320" height="240"></iframe>

setData()

  setData()方法的第一个参数也是数据类型,第二个参数是要放在剪贴板中的文本。对于第一个参数的规则与getData()相同

  在IE浏览器中,该方法在成功将文本放到剪贴板中后,返回true,否则返回false;而其他浏览器中,该方法无返回值

  [注意]在paste事件中,只有IE浏览器可以正常使用setData()方法,chrome浏览器会静默失败,而firefox浏览器会报错

<input id="test" value="123"><script>test.oncopy=function(e){    e = e || event;    var clipboardData = e.clipboardData || window.clipboardData;    clipboardData.setData(text,测试);    test.value = clipboardData.getData(text);    return false;}</script>

<iframe style="width: 100%; height: 40px;" src="http://sandbox.runjs.cn/show/3hvjynkg" frameborder="0" width="320" height="240"></iframe>

clearData()

  clearData()方法用于从剪贴板中删除数据,它接受一个参数,即要取得的数据的格式。在IE中,有两种数据格式:"text"和"URL"。在其他浏览器中,这个参数是一种MIME类型;不过,可以用"text"代表

  在IE浏览器中,该方法在成功将文本放到剪贴板中后,返回true,否则返回false;而其他浏览器该方法的返回值为undefined

  [注意]在paste事件中,chrome浏览器和IE浏览器可以正常使用setData()方法,而firefox浏览器会报错

<input id="test" value="123"><script>test.oncopy=function(e){    e = e || event;    var clipboardData = e.clipboardData || window.clipboardData;    test.value = clipboardData.clearData(text);    return false;}</script>

<iframe style="width: 100%; height: 40px;" src="http://sandbox.runjs.cn/show/cth8p5hh" frameborder="0" width="320" height="240"></iframe>

应用

【1】屏蔽剪贴板

  通过阻止默认行为来屏蔽剪贴板。对于一些受保护的文档来说是一种选择

<input value="text"><button id="test">屏蔽剪贴板</button><script>test.onclick = function(){    document.oncopy=document.oncut = document.onpaste = function(e){        e = e || event;        alert(该文档不允许复制剪贴操作,谢谢配合)        return false;    }    }</script>

<iframe style="width: 100%; height: 40px;" src="http://sandbox.runjs.cn/show/azpmzixw" frameborder="0" width="320" height="240"></iframe>【2】过滤字符

  如果确保粘贴到文本框中的文本中包含某些字符,或者符合某种形式时,可以使用剪贴板事件。比如只允许粘贴数字

<input id="test"><script>test.onpaste = function(e){    e = e || event;    var clipboardData = e.clipboardData || window.clipboardData;    if(!/^\d+$/.test(clipboardData.getData(text)))        return false;    }    }</script>    

<iframe style="width: 100%; height: 40px;" src="http://sandbox.runjs.cn/show/z8tj8hse" frameborder="0" width="320" height="240"></iframe>

<script type="text/javascript">// 0){ return; } if(select[i].getBoundingClientRect().top <= 0 && select[i+1]){ if(select[i+1].getBoundingClientRect().top > 0){ change(oCon.children[i+2]) } }else{ change(oCon.children[select.length+1]) } }}document.body.onmousewheel = wheel;document.body.addEventListener(‘DOMMouseScroll‘,wheel,false);var oCon = document.getElementById("content");var close = oCon.getElementsByTagName(‘span‘)[0];close.onclick = function(){ if(this.innerHTML == ‘显示目录‘){ this.innerHTML = ‘ב; this.style.background = ‘‘; oCon.style.border = ‘2px solid #ccc‘; oCon.style.width = ‘‘; oCon.style.height = ‘‘; oCon.style.overflow = ‘‘; oCon.style.lineHeight = ‘30px‘; }else{ this.innerHTML = ‘显示目录‘; this.style.background = ‘#3399ff‘; oCon.style.border = ‘none‘; oCon.style.width = ‘60px‘; oCon.style.height = ‘30px‘; oCon.style.overflow = ‘hidden‘; oCon.style.lineHeight = ‘‘; }}for(var i = 2; i < oCon.children.length; i++){ oCon.children[i].onmouseover = function(){ this.style.color = ‘#3399ff‘; } oCon.children[i].onmouseout = function(){ this.style.color = ‘inherit‘; if(this.mark){ this.style.color = ‘#3399ff‘; } } oCon.children[i].onclick = function(){ change(this); } }function change(_this){ for(var i = 2; i < oCon.children.length; i++){ oCon.children[i].mark = false; oCon.children[i].style.color = ‘inherit‘; oCon.children[i].style.textDecoration = ‘none‘; oCon.children[i].style.borderColor = ‘transparent‘; } _this.mark = true; _this.style.color = ‘#3399ff‘; _this.style.textDecoration = ‘underline‘; _this.style.borderColor = ‘#2175bc‘; }// ]]></script><script type="text/javascript" src="http://files.cnblogs.com/files/xiaohuochai/contextMenu.js"></script>

深入理解DOM事件类型系列第四篇——剪贴板事件