在线编辑器在我们日常的项目开发中非常有用(如新闻系统),它可以方便地实现文章的在线编辑,省掉了frontpage等工具。那么是怎样实现浏览器在线编辑功能的呢? 首先需要ie的支持,在ie5.5以后就有一个编辑状态. 就是利用这个编辑状态,然后用javascript来控制在线编辑的。
首先要有一个编辑框,这个编辑框其实就是一个可编辑状态的网页, 我们用iframe来建立编辑框。
并且在加上javascript代码来指定htmledit有编辑功能(下面提供完整的原代码):
复制代码 代码如下:
');
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(+sel.text+);
}
function inserthtml(html)
{
if (editor.document.selection.type.tolowercase() != none)
{
editor.document.selection.clear() ;
}
editor.document.selection.createrange().pastehtml(html) ;
}