富文本编辑器原理

   富文本编辑器,Rich Text Editor, 简称 RTE, 是一种可内嵌于浏览器,所见即所得的文本编辑器。
        富文本编辑器不同于文本编辑器,比較好的文本编辑器有kindeditor,fckeditor等,百度推出的开源富文本编辑器UEditor算是当中的后起之秀。
        对于支持富文本编辑的浏览器来说,事实上就是设置 document 的 designMode 属性为 on 后,再通过运行 document.execCommand('commandName'[, UIFlag[, value]]) 就可以。commandName 和 value 能够在MSDN 上和MDC 上找到,它们就是我们创建各种格式的命令,例如说,我们要加粗字体,运行 document.execCommand('bold', false) 就可以。非常easy是吧?可是值得注意的是,一般是选中了文本后才运行命令,被选中的文本才被格式化。对于未选中的文本进行这个命令,各浏览器有不同的处理方式,例如 IE 可能是对位于光标中的标签内容进行格式化,而其他浏览器不做不论什么处理,这超出本文的内容,不细述。同一时候须要注意的是,UIFlag 这个參数设置为 true 表示 display any user interface triggered by the command (if any)

各浏览器之前的差别
1、Mozilla和IE在生成HTML时,Mozilla是生成span样式,而IE则使用HTML标签生成样式:
Mozilla
<span style="font-weight: bold;">I love geckos.</span>
<span style="font-weight: bold; font-style: italic;
text-decoration: underline;">Dinosaurs are big.</span>
IE
<STRONG>I love geckos.</STRONG>
<STRONG><EM><U>Dinosaurs are big.</U></EM></STRONG>
2、另外一个差别就是訪问iframe中的document
Mozilla使用W3C标准方式 IFrameElement.contentDocument
IE使用 IFrameElement.document
或者直接使用 document.getElementById(aID).contentWindow.document 获取document

这里附上最简化的编辑器演示样例:


eidtor.html
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html;charset=UTF-8"/>
  <title></title>
  <style type="text/css">
    .editorIframe {
      border: 1px solid #0000ff;
      width: 100%;
      height: 300px;
    }
  </style>

</head>
<body onload="onload()">
<div>
  自己定义编译器
</div>
<div>
  <button onclick="doRichEditCommand('bold')" style="font-weight:bold; width: 25px">B</button>
  <button onclick="doRichEditCommand('italic')" style="font-weight:bold; width: 25px">I</button>
</div>
<div>
  <iframe id="editorIframe" class="editorIframe" src="editorIframe1.html"></iframe>
</div>

<script type="text/javascript">
  var editorDoc = getIFrameDocument('editorIframe');
  var editorEl = document.getElementById('editorIframe');

  function onload() {
    document.getElementById('editorIframe').contentWindow.focus();
  }


  function doRichEditCommand(cmd, arg) {
    //这里不能使用缓存对象,editorDoc
    cmd && getIFrameDocument('editorIframe').execCommand(cmd, false, arg);
    document.getElementById('editorIframe').contentWindow.focus();
  }

  function getIFrameDocument(aID) {

    return document.getElementById(aID).contentWindow.document;

    //不须要使用Mozilla写法
    // if contentDocument exists, W3C compliant (Mozilla)
//    if (document.getElementById(aID).contentDocument) {
//      return document.getElementById(aID).contentDocument;
//    } else {
//      // IE
//      return document.frames[aID].document;
//    }
  }
</script>
</body>
</html>
eidtorIframe.html

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html;charset=UTF-8"/>
  <title></title>
  <style type="text/css">
    html,body{
      margin: 2;
    }
  </style>
  <script type="text/javascript">
    function onload() {
      window.document.designMode = "On";
      document.body.focus();
    }

  </script>
</head>
<body contentEditable="true" onload="onload()">


</body>
</html>




posted @ 2015-02-28 11:46  blfshiye  阅读(447)  评论(0编辑  收藏  举报