找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 7452|回复: 0

所见即所得富文本编辑器实现原理

[复制链接]
发表于 2011-12-22 11:29:53 | 显示全部楼层 |阅读模式
相信很多人都使用过多种富文本编辑器,富文本编辑器常用于编辑博客、用户交互,富文本编辑器分为两种:所见即所得和非所见即所得
两种富文本编辑器的实现原理是不相同的。

1. 非所见即所得编辑器
这种编辑器的实现原理很简单,用textarea元素就可以实现,假如要实现粗体、斜体、下划线、颜色字、图片的效果,只需在字的中间加上自定义标签即可,例如: 富文本编辑器 ,[img]src=”http://www.google.com.hk/intl/zh-CN/images/logo_cn.png”[img]当然这些规则你得自己通过js进行定制。当POST提交后,再把这些标签转换为html标签。

2. 所见即所得编辑器
在1中我提到的几种效果,我们无法在textarea中见到立竿见影的效果(所见即所得),而文本域本身也只是支持一些字符的输入,并不支持显示html。
如何做到编辑像文本域,又能够即时所见呢?答案就是使用iframe作为内容编辑区域。iframe本身也是一个嵌套页面,它如何能够被编辑呢?这里有一些关键的属性,它们可以做到让iframe可以被编辑。
还是直接上代码来得方便一些,代码也很少。
  1. <html>
  2. <script language="javascript" type="text/javascript">
  3. //初始化编辑器
  4. function init() {
  5. var ifr = document.getElementById("editor");
  6. var doc = ifr.contentDocument || ifr.contentWindow.document; // W3C || IE
  7. doc.designMode="on";
  8. doc.contentEditable= true;
  9. doc.write('<html><head><style>body{ margin:3px; word-wrap:break-word; word-break: break-all; }</style></head><body>GoodNessEditor</body></html>');
  10. alert(doc.body.innerHTML);
  11. }
  12. //设置选定的文本为粗体/正常
  13. function setBold() {
  14. var win=document.getElementById("editor").contentWindow;
  15. win.document.execCommand("Bold",false,null);
  16. win.focus();
  17. }
  18. </script>
  19. <p>
  20.   <input type="button" id="bBtn" value="B" style="font-weight:bold" onclick="setBold();" />
  21. </p>
  22. <p>
  23.   <iframe id="editor" width="600px" height="400px" style="border:solid 1px;"></iframe>
  24. </p>
  25. <script type="text/javascript">
  26. init();
  27. </script>
复制代码
很重要的几点
    (1). 用ifr.contentDocument || ifr.contentWindow.document方式获取iframe的文档对象
    (2). 分别设置designMode属性为’on’,contentEditable属性为’true’让iframe可编辑
    (3). 通过doc.body.innerHTML方法获取内容,这个内容是我们最终要插入到数据库或显示在页面上的(例如用户评论)
大家可以运行上面的代码,可以看到如下截图中的效果:



但是在实际运行的时候,你是否发现不论哪个浏览器打开这个页面都处于正在加载的状态(那个轮子转啊转,转个不停…)
只要在doc.write()方法前后加上doc.open(), doc.close()就可以了(在写之前开启,写完之后关闭)。
最后,我们的Web程序中经常使用jQuery作为基础类库,那就把上面的代码也改造为jQuery吧。代码如下:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>KF富文本编辑器</title>
  6. <script type="text/javascript" src=http://www.cnblogs.com/"jquery.min.js"></script><script type="text/javascript">
  7. $(function(){
  8.         $d = $("#editor")[0].contentWindow.document; // IE、FF都兼容
  9.         $d.designMode="on";
  10.         $d.contentEditable= true;
  11.         $d.open();
  12.         $d.close();
  13.         $("body", $d).append("<div>A</div><div>B</div><div>C</div>");
  14.        
  15.         $('#insert_img').click(function(){
  16.                 // 在iframe中插入一张图片                                                                       
  17.                 var img = '<img src=http://www.cnblogs.com/"' + $('#path').val() +'" />';
  18.                 $("body", $d).append(img);
  19.         });
  20.        
  21.         $('#preview').click(function(){
  22.                 // 获取iframe的body内容,用于显示或者插入到数据库
  23.                 alert($('#editor').contents().find('body').html());
  24.                 $('#preview_area').html($('#editor').contents().find('body').html());
  25.         });
  26. });
  27. </script>
  28. </head>
  29. <body>
  30. <p><iframe id="editor" width="600px" height="200px" style="border:solid 1px;"></iframe></p>
  31. <input type="text" id="path" value="http://www.google.com.hk/intl/zh-CN/images/logo_cn.png"/>
  32. <input type="button" id="insert_img" value="插入图片" />
  33. <input type="button" id="preview" value="预览" />
  34. <p style="border: 1px dashed #ccc;" id="preview_area"></p>
  35. </body>
  36. </html>
复制代码
效果图如下:

是不是觉得很简单呢?

本文链接
您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-5-22 17:53 , Processed in 0.025192 second(s), 5 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表