年前年后一直忙得没完没了,这篇关于自己做的简单功能的html在线编辑器一直想写,直到今天才有机会,以前都是直接把网络上的拿来嵌在iframe中,好用自然不说,版权可就要谨慎了,这次只涉及到几个简单的功能,于是自己试着做了个简单功能的,查了很多资料,也试了很多次,写下总结,就当为以后改进使用留份资料参考,同时我也将继续改进。

添加文章时可以使用如下方式(编辑时必须拆开使用)
function document.onreadystatechange()
{
 HtmlEdit.document.designMode = "On"
}
document.onreadystatechange()相当于body的onload()

//以下语句相当于打开iframe可编辑功能,每次打开时都会重新初始化iframe
HtmlEdit.document.designMode = "On"

//将焦点定位在iframe中,通过selection.createRange()来获得在iframe中选中的区域
HtmlEdit.focus();
var sel = HtmlEdit.document.selection.createRange();

对于selection.createRange()网络上使用的例子多半是
function doB(){ 
 HtmlEdit.focus(); 
 var sel = HtmlEdit.document.selection.createRange(); 
 insertHTML("<b>"+sel.text+"</b>"); 

如果只是几个功能还行,多个,难道写一堆function,直接使用DHTML中的excommand效率更高

//插入各种链接
function format(cmd,opt)
{
 textContent.document.execCommand(cmd,"",opt);
 textContent.focus();
}

在DHTML中
execCommand的解释:
Executes a command on the current document, current selection, or the given range.
bSuccess = object.execCommand(sCommand [, bUserInterface] [, vValue])

selectedIndex的解释:
selectedIndex Property :Sets or retrieves the index of the selected option in a select object


调用此函数的链接代码
       


插入字体颜色、图片、flash、视频的以另一个页面载入


//===================
//调用flash.htm页面
//===================


//=============
//调用的media.htm
//============

//==================
//调用的pic.htm
//==================

//=================
//调用selectcolor.htm
//=================

//==========================
//添加文章内容
//提交iframe中的内容
//==========================
要提交iframe的内容到数据库,必须借助js脚本作为中介进行传递,这就很明显可以感觉到在编辑文章内容,iframe的内容会有少许滞后载入。
定义隐藏的

<form name="form1" method="post" id="getform" action="" onsubmit="ifr_outContent()" ></form>
<input type="hidden" name="tSValue" id="tSValue" value="">

//将iframe中的内容,取出赋予input的tSValue中,待onsubmit后一起提交给后端,进行数据插入操作。
function ifr_outContent()
{
 HtmlEdit.focus();
 //var ifr_window=window.frames["HtmlEdit"]; 
 //var conHtml = ifr_window.document.body; //这两句是获得iframe对象内部非html内容的最佳方法,

 cont=HtmlEdit.document.body.innerHTML;
 document.getElementById("tCValue").value = cont;
}

//====================
//编辑的文章内容
//====================
<form name="form1" method="post" id="getform" action="" onsubmit="ifr_outContent()" ></form>
<input type="hidden" name="tSValue" id="tSValue" value=程序从数据库中获取的值>

function set()
{
 var ifr_window = window.frames["HtmlEdit"];
 var conHtml = ifr_window.document.body;
 conHtml.innerHTML = 程序从数据库中获取的值;
}

注意:我用php取编辑数据时,加了
function txtToEnter($str)
{
 $str = ereg_replace(" "," ",$str);
 //$str = ereg_replace("\r","<br>",$str);
 //$str = ereg_replace("\n","<br>",$str);
 $str = ereg_replace("\r\n","<br>",$str);
 $str  = ereg_replace("'","\"",$str);
 return $str;
}
将从数据库中取出的内容转化了下,要不然一换行就没法显示编辑的数据了,其实应该有js处理换行的方法,还在寻找中。。。

 申明:调用的selectcolor.htm,pic.htm,media.htm,flash.htm文件均来自互联网,如作者发现,请与我联系,标明出处。

posted on 2007-03-22 15:11  林宁  阅读(3633)  评论(3编辑  收藏  举报