新年上班的第一个任务,负责将从word中复制过来的内容过滤掉格式(也就是过滤掉HTML tag),使用js实现,在粘贴键按下时触发过滤事件.详细代码如下:
<script type="text/javascript"> var EditOrAdd = ""; function url(){ content.focus(); document.execCommand("Createlink","ture"); } function color(clr){ content.focus(); document.execCommand('ForeColor',false,clr); } function text(txt){ content.focus(); document.execCommand(txt); } function text2(txt){ stylecontent.focus(); document.execCommand(txt); } function cleanAndPaste( html ) { //过滤所有的HTML标签 2010-02-24 小叶 html = html.replace(/<[^>]*>|<\/[^>]*>/gm, ""); return html; } function keyDown(){ if(window.event.keyCode==27) { event.returnValue=false; return; } //alert("ASCII代码是:"+event.keyCode); if ( (window.event.ctrlKey ) && (window.event.keyCode==86) ) { //过滤word格式 if(EditOrAdd=="Add") { content.innerHTML = cleanAndPaste( content.innerHTML ); } else { stylecontent.innerHTML = cleanAndPaste(stylecontent.innerHTML); } } } function keyUp(){ if(window.event.keyCode==27) { event.returnValue=false; return; } if ( (window.event.ctrlKey ) && (window.event.keyCode==86) ) { if(EditOrAdd=="Add") { content.innerHTML = cleanAndPaste( content.innerHTML ); } else { stylecontent.innerHTML = cleanAndPaste(stylecontent.innerHTML); } } } function changeEdit() { EditOrAdd = "Edit"; } function changeAdd() { EditOrAdd = "Add"; } document.οnkeydοwn=keyDown; document.onkeyup = keyUp; </script>