纯javascript的HTML在线编辑器

因工作需要,需使用 javascript 来操作 HTML 在线编辑器,在网上搜了许多,都是与服务器端相结合的,通过表单提交的方式来处理的。没办法,只好在此基础上对其进行更新! 
参考的myeditor控件不知是哪位高人写的 。在此表示感谢
首先,定义一全局变量,用于向 HtmlEditor 传递值:
var  EDITOR_DEFAULT_VALUE  =   "" ;                 // 全局变量,用于向Editor控件传递值

原先控件的editfunc.js中的代码注掉了很多,删除掉了作为get方式传递的TextArea对象。

editfunc.js 中设置初值的方法,作了改变:
function  fSetHtmlContent()
{
    
var ovalue = window.parent.EDITOR_DEFAULT_VALUE;
    
var html = ovalue;
    
if (html)
    
{
        
var header = "<head><link rel="stylesheet" type="text/css" href="editorArea.css" /></head><body MONOSPACE>" ;
        
var f = window.frames["HtmlEditor"];
        f.document.open();
        
//f.document.getElementsByTagName("BODY")[0].innerHTML = oLinkField.value;
        f.document.write(header + html);
        f.document.close();
    }

}

我是用的动态生成方式,通过点击按钮生成HtmlEditor:

代码如下:

// containername:存放iframe的容器名称 framename:HtmlEditor的iframe名称 editorpath:编辑器index.html的路径
function  createEditor(containername,framename,editorpath)
{
    
if(!document.getElementById(framename))
    
{
        
var HTMLEDITOR = document.createElement("iframe");
        HTMLEDITOR.id 
= framename;
        HTMLEDITOR.name 
= framename;
        HTMLEDITOR.src 
= editorpath;
        HTMLEDITOR.frameBorder 
= "0";
        HTMLEDITOR.marginHeight 
= "0";
        HTMLEDITOR.marginWidth 
= "0";
        HTMLEDITOR.height 
= "238";
        HTMLEDITOR.width 
= "400";
        
        document.getElementById(containername).appendChild(HTMLEDITOR);
    }

}

设置HtmlEditor的值的代码:

// 设置HtmlEditor的文本 framename:HtmlEditor的iframe名称 html_text:带格式的文本
function  setEditorText(framename,html_text)
{    
    HtmlEditor_Default_Value 
= html_text;
    
    
if(window.frames[framename].frames["HtmlEditor"!= null)
    
{
        
var html = window.frames[framename].frames["HtmlEditor"].document.getElementsByTagName("BODY")[0];
        html.innerHTML 
= HtmlEditor_Default_Value;
    }

}

获得HtmlEditor的值的代码:

// 获得HtmlEditor的带格式文本 framename:HtmlEditor的iframe名称
function  getEditorHTML(framename)
{
    
var html = window.frames[framename].frames["HtmlEditor"].document.getElementsByTagName("BODY")[0].innerHTML;
    
if ( (html.toLowerCase() == "<p>&nbsp;</p>"|| (html.toLowerCase() == "<p></p>") )
    
{
        html 
= "";
    }

    
return html;
}
js源代码如下:
var  EDITOR_DEFAULT_VALUE  =   "" ;                 // 全局变量,用于向Editor控件传递值

function  init()
{
    
var obj = document.getElementById("taContent");
    obj.value 
= "<b>粗体</b><i>斜体</i>";
}


// containername:存放iframe的容器名称 framename:HtmlEditor的iframe名称 editorpath:编辑器index.html的路径
function  createEditor(containername,framename,editorpath)
{
    
if(!document.getElementById(framename))
    
{
        
var HTMLEDITOR = document.createElement("iframe");
        HTMLEDITOR.id 
= framename;
        HTMLEDITOR.name 
= framename;
        HTMLEDITOR.src 
= editorpath;
        HTMLEDITOR.frameBorder 
= "0";
        HTMLEDITOR.marginHeight 
= "0";
        HTMLEDITOR.marginWidth 
= "0";
        HTMLEDITOR.height 
= "238";
        HTMLEDITOR.width 
= "400";
        
        document.getElementById(containername).appendChild(HTMLEDITOR);
    }

}


// 设置初始值
function  setEditorDefaultValue(text)
{
    EDITOR_DEFAULT_VALUE 
= text;
}


// 得到textarea的值
function  getTextareaValue()
{
    
var obj = document.getElementById("taContent");
    
return obj.value;
}


// 获得HtmlEditor的带格式文本 framename:HtmlEditor的iframe名称
function  getEditorHTML(framename)
{
    
var html = window.frames[framename].frames["HtmlEditor"].document.getElementsByTagName("BODY")[0].innerHTML;
    
if ( (html.toLowerCase() == "<p>&nbsp;</p>"|| (html.toLowerCase() == "<p></p>") )
    
{
        html 
= "";
    }

    
return html;
}


// 设置HtmlEditor的文本 framename:HtmlEditor的iframe名称 html_text:带格式的文本
function  setEditorText(framename,html_text)
{    
    HtmlEditor_Default_Value 
= html_text;
    
    
if(window.frames[framename].frames["HtmlEditor"!= null)
    
{
        
var html = window.frames[framename].frames["HtmlEditor"].document.getElementsByTagName("BODY")[0];
        html.innerHTML 
= HtmlEditor_Default_Value;
    }

}

源代码: 点此进入下载页面
博客园下载地址: http://www.cnblogs.com/Files/redleaf1995/editor.rar
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值