在线编辑器实现原理(兼容IE和FireFox)
2008-09-25 09:18
381 查看
线编辑器在我们日常的项目开发中非常有用(如新闻系统),它可以方便地实现文章的在线编辑,省掉了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>
首先要有一个编辑框,这个编辑框其实就是一个可编辑状态的网页, 我们用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>
相关文章推荐
- 在线编辑器实现原理(兼容IE和FireFox)
- 简单HTML在线编辑器实现原理(兼容IE和FireFox)(转)
- 在线编辑器的实现原理(兼容IE和FireFox)
- 简单HTML在线编辑器实现原理(兼容IE和FireFox)(转)
- 在线编辑器的实现原理(兼容IE和FireFox)
- 在线编辑器实现原理(兼容IE和FireFox)
- 在线编辑器实现原理(兼容IE和FireFox)
- 纯JavaScript实现的幻灯片(兼容IE和Firefox)
- javascript实现拖动层效果代码(符合标准且兼容IE,chrome,firefox)
- js 实现键盘记录 兼容FireFox和IE
- Js 键盘记录实现(兼容FireFox和IE)
- js实现复制到系统剪贴板之IE,firefox兼容版
- IE, FireFox, Opera 浏览器支持CSS实现Alpha透明的方法 兼容问题
- js 实现键盘记录 兼容FireFox和IE
- js实现iframe框架取值的方法(兼容IE,firefox,chrome等)
- 兼容ie和firefox的鼠标经过(onmouseover和onmouseout)实现--简短版
- JS实现iframe编辑器光标位置插入内容的方法(兼容IE和Firefox)
- 兼容ie和firefox的鼠标经过(onmouseover和onmouseout)实现--简短版
- 兼容IE/Firefox/Opera/Safari的检测页面装载完毕的脚本Ext.onReady的实现
- IE, FireFox, Opera 浏览器支持CSS实现Alpha透明的方法 兼容问题