給網頁編輯器新增paste事件
<script language="javascript">
function _iframeOnload(){
targetDoc = document.frames.target.document;
targetDoc.f=function(e){proxyEvent(e);};
with(targetDoc.body){
attachEvent("onpaste",function(event){document.f(event)});
}
target.document.designMode='On';
}
function proxyEvent(e){
switch(e.type){
case "paste":
fnPaste(e);
break;
}
}
function fnPaste(e) {
e.returnValue = false;
//targetDoc.execCommand('paste');
var shtml= GetClipboardHTML();
target.document.selection.createRange().pasteHTML( shtml) ;
// CleanCode();
}
// 取剪粘板中的HTML格式資料
function GetClipboardHTML() {
var oDiv = document.getElementById("eWebEditor_Temp_HTML")
oDiv.innerHTML = "" ;
var oTextRange = document.body.createTextRange() ;
oTextRange.moveToElementText(oDiv) ;
oTextRange.execCommand("Paste") ;
var sData = oDiv.innerHTML ;
oDiv.innerHTML = "" ;
return sData ;
}
</script>
<IFRAME id=target marginWidth=4 frameborder=1 scrolling=yes style="HEIGHT: 100%; WIDTH: 100%" onload="_iframeOnload()"> </IFRAME>
<div id="eWebEditor_Temp_HTML" style="VISIBILITY: hidden; OVERFLOW: hidden; POSITION: absolute; WIDTH: 1px; HEIGHT: 1px"></div>
<input type="button" id="btn1" value="change" onclick="target.document.body.innerText=target.document.body.innerHTML;" />