在线编辑器实现原理(兼容IE和FireFox)

 

在线编辑器在我们日常的项目开发中非常有用(如新闻系统),它可以方便地实现文章的在线编辑,省掉了FrontPage等工具。那么是怎样实现浏览器在线编辑功能的呢?  首先需要IE的支持,在IE5.5以后就有一个编辑状态. 就是利用这个编辑状态,然后用javascript来控制在线编辑的。
     首先要有一个编辑框,这个编辑框其实就是一个可编辑状态的网页, 我们用iframe来建立编辑框。
     <IFRAME id=“HtmlEdit” style="WIDTH: 100%; HEIGHT: 296px" marginWidth=“0” marginHeight=“0”></IFRAME>

     并且在加上javascript代码来指定HtmlEdit有编辑功能(下面提供完整的原代码):

  <script language="javascript">
     var editor;
     editor = document.getElementById("HtmlEdit").contentWindow;
    
     //只需键入以下设定,iframe立刻变成编辑器。
     editor.document.designMode = 'On';
     editor.document.contentEditable = true;
    
     //但是IE与FireFox有点不同,为了兼容FireFox,所以必须创建一个新的document。
     editor.document.open();
     editor.document.writeln('<html><body></body></html>');
     editor.document.close();

     //字体特效 - 加粗方法一
     function addBold()
     {
     editor.focus();
     //所有字体特效只是使用execComman()就能完成。
     editor.document.execCommand("Bold", false, null);
     }
     //字体特效 - 加粗方法二
     function addBold()
     {
     editor.focus();
     //获得选取的焦点
     var sel = editor.document.selection.createRange();
     insertHTML("<b>"+sel.text+"</b>");
     }
     function insertHTML(html)
     {
         if (editor.document.selection.type.toLowerCase() != "none")
         {
         editor.document.selection.clear() ;
         }
         editor.document.selection.createRange().pasteHTML(html) ;
     }

  </script>


文章来源于 前端开发 | HTML , CSS & JavaScript--mon的博客:http://monw3c.blogbus.com/logs/33641688.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用中提到了在HTML中定义一个iframe元素,用于实现在线编辑器。该iframe元素具有属性frameborder="0"和id="editor"。 引用中提到了使用JavaScript来实现在iframe编辑器中插入内容的方法。该方法可以通过获取iframe元素的document对象,然后使用document的相关方法来插入内容。 在实现iframe在线编辑器时,可以使用以下步骤: 1. 在HTML中定义一个iframe元素,设置其id属性为"editor"。 2. 使用JavaScript获取到该iframe元素,并保存为一个变量,例如eleEditor。 3. 使用eleEditor的contentWindow属性获取到iframe的window对象,保存为一个变量,例如editorWin。 4. 使用editorWin的document属性获取到iframe的document对象,保存为一个变量,例如editorDoc。 5. 可以使用editorDoc的相关方法来操作iframe中的内容,例如插入文本、插入HTML等。 需要注意的是,不同浏览器对于iframe的contentWindow和contentDocument的兼容性有所不同。在引用中提到,对于不需要兼容contentWindow的情况,可以直接使用$('#Editor').get(0).contentWindow来获取iframe的window对象,而对于Firefox和IE8,可以使用$('#Editor').get(0).contentDocument来获取iframe的document对象。 因此,通过以上步骤和引用的内容,可以实现一个基本的iframe在线编辑器。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [iframe实现编辑器](https://blog.csdn.net/wjlotus/article/details/50455020)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [JS实现iframe编辑器光标位置插入内容的方法(兼容IE和Firefox)](https://download.csdn.net/download/weixin_38565631/13139540)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值