js設定剪貼簿資料 event.clipboardData.setData("text/plain",value);不起作用,並非所有的瀏覽器都支援,限制了
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<script type="text/javascript">
var EventUtil={
getClipboardData:function(event){
var clipboardData=(event.clipboardData||window.clipboardData);
return clipboardData.getData("text");
},
setClipboardData:function(event,value){
if(event.clipboardData){
//return event.clipboardData.setData("text",value);
return event.clipboardData.setData("text/plain",value);
}else if(window.clipboardData){
return window.clipboardData.setData("text",value);
}
},
addHandler:function(element,type,handler){
if(element.addEventListener){
element.addEventListener(type,handler,false);
}else if(element.attachEvent){
element.attachEvent("on"+type,handler);
}else{
element["on"+type]=handler;
}
},
getEvent:function(event){
return event?event:window.event;
},
preventDefault:function(event){
if(event.preventDefault)
event.preventDefault();
else
event.returnValue=false;
}
}
function trimall(txt){
/*txt=txt.replace(/^\\s+/,'');
alert(txt);
txt=txt.replace(/\s+$/,'');
alert(txt);*/
txt=txt.replace(/(^\s*)|(\s*$)/g,"");
return txt;
}
function doit(){
/*var txt=document.getElementById("ipt1").value;
alert(txt.length);
txt=trimall(txt);
alert(txt.length);
document.getElementById("ipt1").value=EventUtil.getClipboardData(event);*/
alert(EventUtil.getClipboardData(event));
}
</script>
<body>
<input id="ipt1" />
<input type="button" onclick="javascript:doit();"/>
</body>
<script type="text/javascript">
EventUtil.addHandler(document.getElementById("ipt1"),"paste",function adjustInput(event){
alert(EventUtil.getClipboardData(event));
event=EventUtil.getEvent(event);
var txt=EventUtil.getClipboardData(event);
txt=trimall(txt);
//event.clipboardData.setData("text/plain",value);不起作用,並非所有的瀏覽器都支援,限制了
//var rst=EventUtil.setClipboardData(event,txt);
EventUtil.preventDefault(event);
document.getElementById("ipt1").value=txt;
});
</script>
</html>