你要的是所见即所得HTML编辑器,简单来说需要几个基本步骤:

1,需要一个可以编辑同时又可显效果的编辑框。textarea不行,它只能用来输入纯文本,不能显示颜色、斜体之类的文字样式,就像记事本。你可以使用iframe来实现,修改iframe的designMode属性使其可以被编辑。

<iframe id="myEditer" width="100%" height="150px"></iframe><script>myEditer.document.designMode = 'on';</script>

这样你就可以在这个iframe区域里写字了。

2,选中要添加样式的文字。通常我们用WORD编辑一段文字的样式时,一般是先打字,再编辑样式。所以你需要一个选中要处理文本的方法。JS的selection.createRange()可以选中文本,返回一个对象,你可以通过访问该对象的text属性得到被选中的文本。

<iframe id="myEditer" width="100%" height="150px"></iframe><input type="button" value="加粗" onclick="Bold()"><script>myEditer.document.designMode = 'on';function Bold(){var sel = myEditer.document.selection.createRange();alert(sel.text);}</script>

3,改变被选中文本的样式。selection.createRange()选中文本,返回一个对象,该对象有一个方法execCommand(),可以用来改变被选中文本的样式。

<iframe id="myEditer" width="100%" height="150px"></iframe><input type="button" value="加粗" onclick="Bold()"><script>myEditer.document.designMode = 'on';function Bold(){var sel = myEditer.document.selection.createRange();//alert(sel.text);sel.execCommand("Bold")}</script>

execCommand()的常用用法:字体--宋体、黑体、楷体等execCommand("fontname","",字体)字号--字号大小execCommand("fontsize","",字号)加重execCommand("Bold")斜体execCommand("Italic")下划线execCommand("Underline")删除线execCommand("StrikeThrough")居左execCommand("JustifyLeft")居右execCommand("JustifyRight")居中execCommand("JustifyCenter")剪切execCommand("Cut")拷贝execCommand("Copy")粘贴execCommand("Paste")取消操作--IE5.0以后可以无限取消execCommand("Undo")重复操作execCommand("Redo")

把上面的每个用法用按钮来实现,你就已经完成了一个简单的可视文本编辑器。

document.execCommand()用法说明

View Code

Position 允许通过拖曳移动绝对定位的对象。
AbsolutePosition 设定元素的 position 属性为“absolute”(绝对)。
BackColor 设置或获取当前选中区的背景颜色。
BlockDirLTR 目前尚未支持。
BlockDirRTL 目前尚未支持。
Bold 切换当前选中区的粗体显示与否。
BrowseMode 目前尚未支持。
Copy 将当前选中区复制到剪贴板。
CreateBookmark 创建一个书签锚或获取当前选中区或插入点的书签锚的名称。
CreateLink 在当前选中区上插入超级链接,或显示一个对话框允许用户指定要为当前选中区插入的超级链接的 URL。
Cut 将当前选中区复制到剪贴板并删除之。
Delete 删除当前选中区。
DirLTR 目前尚未支持。
DirRTL 目前尚未支持。
EditMode 目前尚未支持。
FontName 设置或获取当前选中区的字体。
FontSize 设置或获取当前选中区的字体大小。
ForeColor 设置或获取当前选中区的前景(文本)颜色。
FormatBlock 设置当前块格式化标签。
Indent 增加选中文本的缩进。
InlineDirLTR 目前尚未支持。
InlineDirRTL 目前尚未支持。
InsertButton 用按钮控件覆盖当前选中区。
InsertFieldset 用方框覆盖当前选中区。
InsertHorizontalRule 用水平线覆盖当前选中区。
InsertIFrame 用内嵌框架覆盖当前选中区。
InsertImage 用图像覆盖当前选中区。
InsertInputButton 用按钮控件覆盖当前选中区。
InsertInputCheckbox 用复选框控件覆盖当前选中区。
InsertInputFileUpload 用文件上载控件覆盖当前选中区。
InsertInputHidden 插入隐藏控件覆盖当前选中区。
InsertInputImage 用图像控件覆盖当前选中区。
InsertInputPassword 用密码控件覆盖当前选中区。
InsertInputRadio 用单选钮控件覆盖当前选中区。
InsertInputReset 用重置控件覆盖当前选中区。
InsertInputSubmit 用提交控件覆盖当前选中区。
InsertInputText 用文本控件覆盖当前选中区。
InsertMarquee 用空字幕覆盖当前选中区。
InsertOrderedList 切换当前选中区是编号列表还是常规格式化块。
InsertParagraph 用换行覆盖当前选中区。
InsertSelectDropdown 用下拉框控件覆盖当前选中区。
InsertSelectListbox 用列表框控件覆盖当前选中区。
InsertTextArea 用多行文本输入控件覆盖当前选中区。
InsertUnorderedList 切换当前选中区是项目符号列表还是常规格式化块。
Italic 切换当前选中区斜体显示与否。
JustifyCenter 将当前选中区在所在格式化块置中。
JustifyFull 目前尚未支持。
JustifyLeft 将当前选中区所在格式化块左对齐。
JustifyNone 目前尚未支持。
JustifyRight 将当前选中区所在格式化块右对齐。
LiveResize 迫使 MSHTML 编辑器在缩放或移动过程中持续更新元素外观,而不是只在移动或缩放完成后更新。
MultipleSelection 允许当用户按住 Shift 或 Ctrl 键时一次选中多于一个站点可选元素。
Open 打开。
Outdent 减少选中区所在格式化块的缩进。
OverWrite 切换文本状态的插入和覆盖。
Paste 用剪贴板内容覆盖当前选中区。
PlayImage 目前尚未支持。
Print 打开打印对话框以便用户可以打印当前页。
Redo 重做。
Refresh 刷新当前文档。
RemoveFormat 从当前选中区中删除格式化标签。
RemoveParaFormat 目前尚未支持。
SaveAs 将当前 Web 页面保存为文件。
SelectAll 选中整个文档。
SizeToControl 目前尚未支持。
SizeToControlHeight 目前尚未支持。
SizeToControlWidth 目前尚未支持。
Stop 停止。
StopImage 目前尚未支持。
StrikeThrough 目前尚未支持。
Subscript 目前尚未支持。
Superscript 目前尚未支持。
UnBookmark 从当前选中区中删除全部书签。
Underline 切换当前选中区的下划线显示与否。
Undo 撤消。
Unlink 从当前选中区中删除全部超级链接。
Unselect 清除当前选中区的选中状态。

一个简单的开源编辑器:http://www.jb51.net/article/22346.htm

View Code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<metahttp-equiv="Content-Type"content="text/html; charset=utf-8" />
<title>Editor</title>
<styletype="text/css">body{font-size:12px;}#ed{height:300px;width:800px;background-color:#fff}.sssss{background-image:url(http://www.zzsky.cn/build/images/20099493121.gif)}.tag{background-image:url(http://www.zzsky.cn/build/images/20099493121.gif);height:22px;display:inline-table;float:left;;cursor:pointer;margin-top:8px;margin-left:5px;margin-right:2px;vertical-align:middle;line-height:20px;}.span0_hover{background-position:-0px -2px;width:22px}.span1_hover{background-position:-30px -2px;width:22px}.span2_hover{background-position:-58px -2px;width:22px}.span3_hover{background-position:-86px -57px;width:73px}.span4_hover{background-position:-86px -28px;width:73px}.span5_hover{background-position:-164px -2px;width:22px;background-color:#000000}.span6_hover{background-position:-194px -2px;width:22px}.span7_hover{background-position:-45px -192px;width:22px}.span8_hover{background-position:-76px -192px;width:22px}.span9_hover{background-position:-58px -247px;width:22px}.span10_hover{background-position:-86px -247px;width:22px}.span11_hover{background-position:-113px -247px;width:22px}.span12_hover{background-position:-476px -192px;width:22px}.span13_hover{background-position:-505px -192px;width:22px}.span14_hover{background-position:-333px -247px;width:22px}.span15_hover{background-position:-532px -192px;width:22px}.span16_hover{background-position:-560px -192px;width:22px}.span17_hover{background-position:-455px -247px;width:22px}.span18_hover{background-position:-222px -2px;width:73px}.span19_hover{background-position:-380px -2px;width:74px}.span20_hover{background-position:-460px -2px;width:71px}.span0{background-position:-0px -57px;width:22px}.span1{background-position:-30px -57px;width:22px}.span2{background-position:-58px -57px;width:22px}.span3{background-position:-86px -57px;width:73px}.span4{background-position:-86px -28px;width:73px}.span5{background-position:-164px -57px;width:22px;background-color:#000000}.span6{background-position:-194px -57px;width:22px}.span7{background-position:-45px -84px;width:22px}.span8{background-position:-76px -84px;width:22px}.span9{background-position:-58px -140px;width:22px}.span10{background-position:-86px -140px;width:22px}.span11{background-position:-113px -140px;width:22px}.span12{background-position:-476px -84px;width:22px}.span13{background-position:-505px -84px;width:22px}.span14{background-position:-333px -140px;width:22px}.span15{background-position:-532px -84px;width:22px}.span16{background-position:-560px -84px;width:22px}.span17{background-position:-455px -140px;width:22px}.span18{background-position:-222px -57px;width:73px}.span19{background-position:-380px -57px;width:74px}.span20{background-position:-460px -57px;width:71px}.span0_active{background-position:-0px -28px;width:22px}.span1_active{background-position:-30px -28px;width:22px}.span2_active{background-position:-58px -28px;width:22px}.span3_active{background-position:-476px -299px;width:22px}.span4_active{background-position:-505px -299px;width:22px}.ebody{height:auto;width:760px;border:1px solid #999999}.ebar{width:100%;height:36px;background-color:#F0F2F5;};
.edit{height:550px;width:100%;border:0px;}.popupfont{width:200px;height:auto;border:1px solid #7B9EBD;background-color:#F7F7F7;position:absolute;padding:3px; }a.c1{width:96%;height:auto;font-size:10px;display:block;border:1px solid #F7F7F7;padding:3px;color:#666666;text-decoration:none;}a.c1:hover{width:96%;height:auto;font-size:10px;border:1px solid #FFCF00;display:block;background-color:#F7EBBD;padding:3px;color:#666666;text-decoration:none;}a.c2{width:96%;height:auto;font-size:12px;display:block;border:1px solid #F7F7F7;padding:3px;color:#666666;text-decoration:none;}a.c2:hover{width:96%;height:auto;font-size:12px;border:1px solid #FFCF00;display:block;background-color:#F7EBBD;padding:3px;color:#666666;text-decoration:none;}a.c3{width:96%;height:auto;font-size:14px;display:block;border:1px solid #F7F7F7;padding:3px;color:#666666;text-decoration:none;}a.c3:hover{width:96%;height:auto;font-size:14px;border:1px solid #FFCF00;display:block;background-color:#F7EBBD;padding:3px;color:#666666;text-decoration:none;}a.c4{width:96%;height:auto;font-size:16px;display:block;border:1px solid #F7F7F7;padding:3px;color:#666666;text-decoration:none;}a.c4:hover{width:96%;height:auto;font-size:16px;border:1px solid #FFCF00;display:block;background-color:#F7EBBD;padding:3px;color:#666666;text-decoration:none;}a.c5{width:96%;height:auto;font-size:18px;display:block;border:1px solid #F7F7F7;padding:3px;color:#666666;text-decoration:none;}a.c5:hover{width:96%;height:auto;font-size:18px;border:1px solid #FFCF00;display:block;background-color:#F7EBBD;padding:3px;color:#666666;text-decoration:none;}a.c6{width:96%;height:auto;font-size:22px;display:block;border:1px solid #F7F7F7;padding:3px;color:#666666;text-decoration:none;}a.c6:hover{width:96%;height:auto;font-size:22px;border:1px solid #FFCF00;display:block;background-color:#F7EBBD;padding:3px;color:#666666;text-decoration:none;}a.c7{width:96%;height:auto;font-size:26px;display:block;border:1px solid #F7F7F7;padding:3px;color:#666666;text-decoration:none;}a.c7:hover{width:96%;height:auto;font-size:26px;border:1px solid #FFCF00;display:block;background-color:#F7EBBD;padding:3px;color:#666666;text-decoration:none;}a.n{width:96%;height:auto;font-size:14px;display:block;border:1px solid #F7F7F7;padding:3px;color:#666666;text-decoration:none; }a.n:hover{width:96%;height:auto;font-size:14px;border:1px solid #FFCF00;display:block;background-color:#F7EBBD;padding:3px;color:#666666;text-decoration:none;}.textarea{border:0px;display:none;}.toolbarlayer{position:absolute;opacity:0.7;filter:alpha(opacity:70);background-color:#ffffff;height:36px}.bottom{height:30px;width:100%;background-color:#F7F3F7;font-size:12px;}.checkbox{margin-top:10px;margin-top/*\**/:6px\9;margin-left:20px;margin-left/*\**/:16px\9;}.pp{height:auto;border:1px solid #D3D3D3;position:absolute;z-index:5;}.ppt{height:24px;width:100%;background-image:url(http://album.hi.csdn.net/app_uploads/wtcsy/20090726/203207031.p.gif);font-size:12px;font-weight:bold;vertical-align:middle;line-height:24px;
}
</style>
</head>
<body> <divid='ss'></div><divid='sss'></div>
<scripttype="text/javascript">
varSys=(function(ua) {vars={};s.IE=ua.match(/msie ([\d.]+)/)? true:false;s.Firefox=ua.match(/firefox\/([\d.]+)/)? true:false;s.Chrome=ua.match(/chrome\/([\d.]+)/)? true:false;s.IE6=(s.IE&&([/MSIE (\d)\.0/i.exec(navigator.userAgent)][0][1]== 6))? true:false;s.IE7=(s.IE&&([/MSIE (\d)\.0/i.exec(navigator.userAgent)][0][1]== 7))? true:false;s.IE8=(s.IE&&([/MSIE (\d)\.0/i.exec(navigator.userAgent)][0][1]== 8))? true:false;returns;
})(navigator.userAgent.toLowerCase());
Sys.IE6&&document.execCommand("BackgroundImageCache",false,true);var$= function(id) {return "string" == typeofid?document.getElementById(id) : id;
};functionEach(list, fun) {for(vari= 0, len=list.length; i<len; i++) {fun(list[i], i);}
};varCss= function(e, o) {if(typeofo== "string") {e.style.cssText=o;return;}for(variino)e.style[i]=o[i];
};varAttr= function(e, o) {for(variino)e.setAttribute(i, o[i]);
};var$$= function(p, e) {returnp.getElementsByTagName(e);
};functioncreate(e, p, fn) {varelement=document.createElement(e);p&&p.appendChild(element);fn&&fn(element);returnelement;
};functiongetTarget(e) {e=e||window.event;returne.srcElement||e.target;
};functioncreatetab(tri, tdi, arisetab, arisetr, arisetd, p) {vartable=p?p.createElement("table") : create("table");arisetab&&arisetab(table);vartbody=p?p.createElement("tbody") : create("tbody");for(vari= 0; i<tri; i++) {vartr=p?p.createElement("tr") : create("tr");arisetr&&arisetr(i, tr);for(varj= 0; j<tdi; j++) {vartd=p?p.createElement("td") : create("td");arisetd&&arisetd(i, j, td);tr.appendChild(td);}tbody.appendChild(tr);}table.appendChild(tbody);returntable;
};varExtend= function(destination, source) {for(varpropertyinsource) {destination[property]=source[property];}
};varBind= function(object, fun) {varargs=Array.prototype.slice.call(arguments).slice(2);return function() {returnfun.apply(object, args);}
};varBindAsEventListener= function(object, fun, args) {varargs=Array.prototype.slice.call(arguments).slice(2);return function(event) {returnfun.apply(object, [event||window.event].concat(args));}
};varCurrentStyle= function(element) {returnelement.currentStyle||document.defaultView.getComputedStyle(element,null);
};varGetpos= function(o) {varx= 0,y= 0;do{x+=o.offsetLeft;y+=o.offsetTop;}while(( o=o . offsetParent ));return{'x': x,'y': y};
};functionaddListener(element, e, fn) {element.addEventListener?element.addEventListener(e, fn,false) : element.attachEvent("on" +e, fn);
};functionremoveListener(element, e, fn) {element.removeEventListener?element.removeEventListener(e, fn,false) : element.detachEvent("on" +e, fn);
};varClass= function(properties) {var_class= function() {return(arguments[0]!== null && this.initialize&& typeof(this.initialize)== 'function')? this.initialize.apply(this, arguments) :this;};_class.prototype=properties;return_class;
};varEditer= newClass({options: {width:890,height:700,facebg: [{bgimg:"-4px -4px",title:"微笑",wl:22,src:"http://album.hi.csdn.net/app_uploads/wtcsy/20090719/220846596.p.gif"},{bgimg:"-31px -4px",title:"大笑",wl:22,src:"http://album.hi.csdn.net/app_uploads/wtcsy/20090719/220859814.p.gif"},{bgimg:"-58px -4px",title:"窃笑",wl:22,src:"http://album.hi.csdn.net/app_uploads/wtcsy/20090719/220911971.p.gif"},{bgimg:"-85px -4px",title:"眨眼",wl:22,src:"http://album.hi.csdn.net/app_uploads/wtcsy/20090719/220928549.p.gif"},{bgimg:"-112px -4px",title:"鬼脸",wl:22,src:"http://album.hi.csdn.net/app_uploads/wtcsy/20090719/220928549.p.gif"},{bgimg:"-139px -4px",title:"色色",wl:22,src:"http://album.hi.csdn.net/app_uploads/wtcsy/20090719/220951955.p.gif"},{bgimg:"-167px -4px",title:"暴牙",wl:22,src:"http://album.hi.csdn.net/app_uploads/wtcsy/20090719/220958111.p.gif"},{bgimg:"-194px -4px",title:"讨厌",wl:22,src:"http://album.hi.csdn.net/app_uploads/wtcsy/20090719/221004564.p.gif"}],fontsizedata: {fontSize: ["字号1","字号2","字号3","字号4","字号5","字号6","字号7"],fontname: ["宋体","黑体","楷体","隶书","幼圆","Arial","Georgia","Verdana","Helvetica"]},oninit:function() {}},initialize:function(container, data, b, options) {this.container=container;this._body=create("div", container);this.toolbar=create("div",this._body);//工具栏this.iframe=create("iframe",this._body);//编辑区this.textarea=create("textarea",this._body);//显示源代码的框框this.bottom=create("div",this._body);//底部this.lightbox=b;//lightboxthis.original=create("input");//显示源代码的按纽this.ed= null;this.isfocus= false;this.option={};Extend(this.option,this.options);Extend(this.option, options);this.rang= null;//选中区this.Fpop= null;this.option.oninit();this.toolbar.className= "ebar";this._body.className= "ebody";this.textarea.className= "textarea";Css(this._body, {width:this.option.width+ "px",height:this.option.height+ "px"});Css(this.textarea, {width: Sys.IE? this.option.width- 2 + "px":this.option.width+ "px",height: Sys.IE? this.option.height- 66 + "px":this.option.height- 70 + "px"});Attr(this.iframe, {src:"about:blank",width: Sys.IE? this.option.width:this.option.width- 4,height: Sys.IE? this.option.height- 66:this.option.height- 70});Attr(this.original, {type:"checkbox",className:"checkbox",align:"top"});this.bottom.className= "bottom";this.bottom.innerHTML= "<span>显示源代码</span>";this.bottom.insertBefore(this.original,this.bottom.childNodes[0]);this.ed=Sys.IE? this.iframe.contentWindow.document:this.iframe.contentDocument;this.ed.open();vardiv=Sys.IE? "<div></div>":"";this.ed.write("<html><head><style>body{margin:5px;font-size:16px;word-wrap:break-word}</style></head><body id='my_body'>" +div+ "</body></html>");this.ed.close();this.ed.contentEditable= true;this.ed.designMode= "on";//设置编辑区为可编辑for(vari= 0, l=data.length; i<l; i++) {varo=create("span",this.toolbar);Attr(o, {title: data[i].title,active:false,unselectable:"on"});o.className= "tag" +data[i].Class;addListener(o,"mouseover", Bind(this,this.Changebgcolor, o, data[i].hover));addListener(o,"mouseout", Bind(this,this.Changebgcolor, o, data[i].Class));addListener(o,"click", Bind(this,this[data[i].action], o, data[i].args));}//生成工具栏
addListener(this.iframe.contentWindow,"focus", Bind(this,function() {this.isfocus= true;}));addListener(this.iframe.contentWindow,"blur", Bind(this,function() {this.isfocus= false;}));addListener(this.ed,'mousedown', Bind(this,this.Show));addListener(this.ed,'mouseup', Bind(this,this.Show));//这2个事件是来判断光标所在位置是否别编辑
addListener(this.original,'click', Bind(this,this.Showoriginal,this.original));//显示源代码
},Changebgcolor:function(o, name, p) {if(o.active)return;o.className= "tag" +name;},Show:function() {varspans=$$(this.toolbar,"span"),Is;varelm=[[spans[0],"Bold","tag span0_active","tag span0"], [spans[1],"Italic","tag span1_active","tag span1"], [spans[2],"Underline","tag span2_active","tag span2"], [spans[12],"InsertUnorderedList","tag span3_active","tag span12"], [spans[13],"InsertOrderedList","tag span4_active","tag span13"]];for(vari= 0, l=elm.length; i<l; i++) {Is= this.ed.queryCommandState(elm[i][1]);elm[i][0].className=Is?elm[i][2] : elm[i][3];elm[i][0].active=Is;}},Showoriginal:function(o) {if(!this.toolbarlayer) {this.toolbarlayer=create("div", document.body);this.toolbarlayer.className= "toolbarlayer";varpos=Getpos(this.toolbar);Css(this.toolbarlayer, {width:this.option.width+ "px",left: pos.x+ "px",top: pos.y+ "px"});}if(o.checked) {this.iframe.style.display= "none";this.textarea.style.display= "block";this.toolbarlayer.style.display= "block";this.textarea.value= this.ed.body.innerHTML;}else{this.iframe.style.display= "block";this.textarea.style.display= "none";this.toolbarlayer.style.display= "none";this.ed.body.innerHTML= this.textarea.value;}},Exec:function(o, cmd, para) {try{this.ed.execCommand(cmd,false, para);this.Show();}catch(err) {return;}},InsertImage:function(o) {if(Sys.IE) {! this.isfocus&& this.iframe.contentWindow.focus();this.rang= this.ed.selection.createRange();}this.lightbox.Show();this.makebody(this.Imagepopoup,"350px","插入图片","InsertImage","Imagepopoup");},CreateLink:function(o) {if(Sys.IE)this.rang= this.ed.selection.createRange();this.lightbox.Show();this.makebody(this.Linkpopoup,"350px","插入连接","CreateLink","Linkpopoup");},Fontcolor:function(o) {varself= this;if(!this.fontcolorpopup) {varcolor= newpopoup({width:"210px",title:"颜色选择"});this.fontcolorpopup=color.container;varpos=Getpos(o);Css(color.container, {left: pos.x+ "px",top: pos.y+o.offsetHeight+ "px"});varbgcolor=["00","33","66","99","CC","FF","00","33","66","99","CC","FF"];$$(color.container,"div")[1].appendChild(createtab(12,18,function(tab) {Attr(tab, {cellPadding:0,cellSpacing:1,border:0,bgColor:"#333333"});},null,function(i, j, td) {varcolor=i< 6 ? "#" +bgcolor[Math.floor(j/ 6)]+bgcolor[Math.floor(j% 6)]+bgcolor[i] :"#" +bgcolor[Math.floor(j/ 6)+ 3]+bgcolor[Math.floor(j% 6)]+bgcolor[i- 6];Attr(td, {width:10,height:10,unselectable:"on"});td.style.backgroundColor=color;addListener(td,'click', Bind(self, self.Execa, color,"fontColor"));addListener(td,'mousedown', BindAsEventListener(self, self.Bubble));}))}else this.fontcolorpopup.style.display= "block";this.Fpop=Bind(this,this.Hide);addListener(this.ed,'click',this.Fpop);addListener(document,'mousedown',this.Fpop);},Expression:function(o) {varself= this;if(!this.facebgpopup) {varexpr= newpopoup({width:"190px",title:"插入表情"});this.facebgpopup=expr.container;varpos=Getpos(o);Css(expr.container, {left: pos.x+ "px",top: pos.y+o.offsetHeight+ "px"});$$(expr.container,"div")[1].appendChild(createtab(1,this.option.facebg.length,function(tab) {Attr(tab, {cellPadding:0,cellSpacing:1,border:0,bgColor:"#FFFFFF"});},null,function(i, j, td) {Css(td, {backgroundImage:"url(http://album.hi.csdn.net/app_uploads/wtcsy/20090719/220510752.p.gif)",backgroundPosition: self.option.facebg[j].bgimg});Attr(td, {width: self.option.facebg[i].wl,height: self.option.facebg[i].wl,unselectable:"on"});addListener(td,'click', Bind(self, self.Execa, self.option.facebg[j].src,"Expression"));addListener(td,'mousedown', BindAsEventListener(self, self.Bubble))}));}else this.facebgpopup.style.display= "block";this.Fpop=Bind(this,this.Hide);addListener(this.ed,'click',this.Fpop);addListener(document,'mousedown',this.Fpop);},Layout:function() {varchild= this.ed.body.childNodes;for(vari= 0, l=child.length; i<l; i++) {if(child[i].nodeName== "DIV" ||child[i].nodeName== "P") child[i].style.textIndent=child[i].style.textIndent== "2em" ? "":"2em";}},Fontsize:function(o) {if(!this.fontsizepopup) {varsize= newpopoup({width:"210px",title:"字号"}),a;this.fontsizepopup=size.container;varpos=Getpos(o);Css(size.container, {left: pos.x+ "px",top: pos.y+o.offsetHeight+ "px"});for(vari= 0, l= this.option.fontsizedata.fontSize.length; i<l; i++) {a=create("a", $$(size.container,"div")[1]);a.className= "c" +(i+ 1);a.setAttribute("href","javascript:void(0);")a.innerHTML= this.option.fontsizedata.fontSize[i];addListener(a,"click", Bind(this,this.Execa, i,"fontSize"));addListener(a,'mousedown', BindAsEventListener(this,this.Bubble));}}else this.fontsizepopup.style.display= "block";this.Fpop=Bind(this,this.Hide);addListener(this.ed,'click',this.Fpop);addListener(document,'mousedown',this.Fpop);},FontName:function(o) {if(!this.fontnamepopup) {varname= newpopoup({width:"200px",title:"字体"}),a;this.fontnamepopup=name.container;varpos=Getpos(o);Css(name.container, {left: pos.x+ "px",top: pos.y+o.offsetHeight+ "px"});for(vari= 0, l= this.option.fontsizedata.fontname.length; i<l; i++) {a=create("a", $$(name.container,"div")[1]);a.className= "n";a.setAttribute("href","javascript:void(0);");a.innerHTML= this.option.fontsizedata.fontname[i];addListener(a,"click", Bind(this,this.Execa,this.option.fontsizedata.fontname[i],"fontname"));addListener(a,'mousedown', BindAsEventListener(this,this.Bubble));}}else this.fontnamepopup.style.display= "block";this.Fpop=Bind(this,this.Hide);addListener(this.ed,'click',this.Fpop);addListener(document,'mousedown',this.Fpop);},makebody:function(o, w, t, f, n) {if(!o) {varself= this;this[n]= newpopoup({width: w,title: t});this[n].pos();$$(this[n].container,"div")[1].innerHTML= "<div style=' margin-top:6px; margin-left:10px'><span >连接地址</span> <input style='width:200px;' type='text' /></div><div style='text-align:center; padding-top:15px;'><img src='http://album.hi.csdn.net/app_uploads/wtcsy/20090719/220836549.p.gif'> <img src='http://album.hi.csdn.net/app_uploads/wtcsy/20090719/220726721.p.gif'></div>"this[n].elm=[$$(this[n].container,"input")[0], $$(this[n].container,"img")[0], $$(this[n].container,"img")[1]];this[n].elm[0].focus()addListener(this[n].elm[1],'click', Bind(this,this.Execa,null, f));addListener(this[n].elm[2],'click',function() {self.lightbox.Close();self[n].Close();});}else with(this[n]) {pos();Show();elm[0].value= "";elm[0].focus();}},Addtable:function() {if(Sys.IE) {! this.isfocus&& this.iframe.contentWindow.focus();this.rang= this.ed.selection.createRange();}this.lightbox.Show();if(Sys.IE)this.rang= this.ed.selection.createRange();if(!this.tablepopup) {varself= this;this.tablepopup= newpopoup({width:"300px",title:"插入表格"});this.tablepopup.pos();$$(this.tablepopup.container,"div")[1].innerHTML= "<div style='margin-left:30px; margin-top:7px;'>行数:<input style='width:50px; height:13px' type='text' /> 列数:<input style='width:50px;height:13px' type='text' /></div><div style=' margin-left:30px; margin-top:7px;'>表格的宽度:<input style='width:50px; height:13px' type='text' /> px</div><div style=' margin-left:30px; margin-top:7px;'>表行的高度:<input style='width:50px; height:13px' type='text' /> px<div style='text-align:center; margin-left:30px; margin-top:7px;'><img src='http://album.hi.csdn.net/app_uploads/wtcsy/20090719/220836549.p.gif'> <img src='http://album.hi.csdn.net/app_uploads/wtcsy/20090719/220726721.p.gif'></div>"varo=$$(this.tablepopup.container,"input");this.tablepopup.elm=[o[0], o[1], o[2], o[3], $$(this.tablepopup.container,"img")[0], $$(this.tablepopup.container,"img")[1]];addListener(this.tablepopup.elm[4],'click', Bind(this,this.Execa,null,"CreateTable"));addListener(this.tablepopup.elm[5],'click',function() {self.lightbox.Close();self.tablepopup.Close();});}else with(this.tablepopup) {pos();Show();elm[0].focus();}this.Fpop=Bind(this,this.Hide);addListener(this.ed,'click',this.Fpop);addListener(document,'mousedown',this.Fpop);},Hide:function(o) {this.facebgpopup&&(this.facebgpopup.style.display= "none");this.fontsizepopup&&(this.fontsizepopup.style.display= "none");this.fontnamepopup&&(this.fontnamepopup.style.display= "none");this.fontcolorpopup&&(this.fontcolorpopup.style.display= "none");removeListener(this.ed,'click',this.Fpop);removeListener(document,'mousedown',this.Fpop);},Bubble:function(e) {if(Sys.IE) {e.cancelBubble= true}else{e.stopPropagation()}},Execa:function(num, stamp) {varexec={fontname:function() {this.fontnamepopup.style.display= "none";this.ed.execCommand('FontName',false, num);},fontSize:function() {this.fontsizepopup.style.display= "none";this.ed.execCommand("FontSize",false, num+ 1)},fontColor:function() {this.fontcolorpopup.style.display= "none";this.ed.execCommand("ForeColor",false, num);},CreateLink:function() {this.lightbox.Close();this.Linkpopoup.Close();if(this.Linkpopoup.elm[0].value== "")return;if(Sys.IE) {this.rang.execCommand("CreateLink",false,this.Linkpopoup.elm[0].value);this.rang.parentElement().target= "_blank";}else{this.ed.execCommand("CreateLink",false,this.Linkpopoup.elm[0].value);this.rang= this.iframe.contentWindow.getSelection().getRangeAt(0);this.rang.commonAncestorContainer.parentNode.target= "_blank";}},InsertImage:function() {this.lightbox.Close();this.Imagepopoup.Close();if(this.Imagepopoup.elm[0].value== "")return;Sys.IE? this.rang.execCommand("InsertImage",false,this.Imagepopoup.elm[0].value) :this.ed.execCommand("InsertImage",false,this.Imagepopoup.elm[0].value);},Expression:function() {this.facebgpopup.style.display= "none";Sys.IE&&(this.iframe.contentWindow.focus());this.ed.execCommand("InsertImage",false, num);},CreateTable:function() {this.lightbox.Close();this.tablepopup.Close();varo= this.tablepopup.elm,p= null;if(Sys.IE) {this.rang.execCommand("InsertImage",false,"http://xxx.com/xxxxx.gif");p= this.rang.parentElement();}else{this.ed.execCommand("InsertImage",false,"http://xxx.com/xxxxx.gif");p= this.iframe.contentWindow.getSelection().getRangeAt(0).commonAncestorContainer;}vartab=createtab(o[0].value, o[1].value,function(tab) {Attr(tab, {cellPadding:0,cellSpacing:1,border:0,bgColor:"#CCCCCC",width: o[2].value});},null,function(i, j, td) {td.height=o[3].value;td.width=o[2].value/o[1].value;td.style.backgroundColor= "#FFFFFF"},this.ed);varimgs=p.getElementsByTagName("img");for(vari= 0, l=p.childNodes.length; i<l; i++) {if(imgs[i].src== "http://xxx.com/xxxxx.gif") {p.replaceChild(tab, imgs[i])}}p.insertBefore(this.ed.createElement("br"), tab.nextSibling);}};Bind(this, exec[stamp])();}
});varpopoup= newClass({options: {width:"200px",title:"标题"},initialize:function(options) {this.container=create("div", document.body);Extend(this.options, options);this.title= this.options.title;this.container.className= "pp";this.container.style.width= this.options.width;this.container.innerHTML= "<div class='ppt'><span style='margin-left:8px;'></span></div><div style='height:auto; width:auto; padding:7px; background-color:#FFFFFF'></div>";this.w= this.container.offsetWidth;this.h= this.container.offsetHeight;$$(this.container,"span")[0].innerHTML= this.title;},pos:function() {varstr= "left:" +(Math.max(document.documentElement.scrollWidth, document.documentElement.clientWidth)- this.w)/ 2 + "px;top:" +((Math.min(document.documentElement.scrollHeight, document.documentElement.clientHeight)- this.h)/ 2 +document.documentElement.scrollTop)+ "px"Css(this.container, str);},Show:function() {this.container.style.display= "";},Close:function() {this.container.style.display= "none";}
})varLightBox={obj:null,config: {Color:"#fff",Opacity:80,zIndex:5},init:function(options) {Extend(this.config, options||{});Extend(this,this.config);delete this.config;this.obj=document.body.insertBefore(document.createElement("div"), document.body.childNodes[0]);varstr= "display:none; z-index:" + this.zIndex+ ";left:0px;top:0px;position:fixed;width:100%;height:100%;background-color:" + this.Color+(Sys.IE? ";filter : alpha(opacity:" + this.Opacity+ ")":";opacity :" + this.Opacity/ 100);Css(this.obj, str);if(Sys.IE6) {this.obj.style.position= "absolute";var_self= this;this._resize= function() {_self.obj.style.width=Math.max(document.documentElement.scrollWidth, document.documentElement.clientWidth)+ "px";_self.obj.style.height=Math.max(document.documentElement.scrollHeight, document.documentElement.clientHeight)+ "px";};this.obj.innerHTML= '<iframe style="position:absolute;top:0;left:0;width:100%;height:100%;filter:alpha(opacity=0);"></iframe>';}return this;},Show:function() {if(Sys.IE6) {this._resize();addListener(window,"resize",this._resize);}this.obj.style.display= "block";},Close:function() {this.obj.style.display= "none";if(Sys.IE6) removeListener(window,"resize", LightBox._resize);}
}
window.onload= function() {vardata=[{Class:"span0",hover:"span0_hover",title:"加粗",action:"Exec",args:"bold"},{Class:"span1",hover:"span1_hover",title:"斜体",action:"Exec",args:"Italic"},{Class:"span2",hover:"span2_hover",title:"下划线",action:"Exec",args:"Underline"},{Class:"span3",hover:"span3_hover",title:"字号",action:"Fontsize",args:null},{Class:"span4",hover:"span4_hover",title:"字体",action:"FontName",args:null},{Class:"span5",hover:"span5_hover",title:"文字颜色",action:"Fontcolor",args:null},{Class:"span6",hover:"span6_hover",title:"插入链接",action:"CreateLink",args:null},{Class:"span7",hover:"span7_hover",title:"剪贴",action:"Exec",args:"Cut"},{Class:"span8",hover:"span8_hover",title:"复制",action:"Exec",args:"Copy"},{Class:"span9",hover:"span9_hover",title:"左对齐",action:"Exec",args:"JustifyLeft"},{Class:"span10",hover:"span10_hover",title:"居中对齐",action:"Exec",args:"JustifyCenter"},{Class:"span11",hover:"span11_hover",title:"右对齐",action:"Exec",args:"JustifyRight"},{Class:"span12",hover:"span12_hover",title:"项目符号",action:"Exec",args:"InsertUnorderedList"},{Class:"span13",hover:"span13_hover",title:"编号",action:"Exec",args:"InsertOrderedList"},{Class:"span14",hover:"span14_hover",title:"插入表格",action:"Addtable",args:null},{Class:"span15",hover:"span15_hover",title:"减少缩进",action:"Exec",args:"Outdent"},{Class:"span16",hover:"span16_hover",title:"增加缩进",action:"Exec",args:"Indent"},{Class:"span17",hover:"span17_hover",title:"清除样式",action:"Exec",args:"RemoveFormat"},{Class:"span18",hover:"span18_hover",title:"插入图片",action:"InsertImage",args:null},{Class:"span19",hover:"span19_hover",title:"插入表情",action:"Expression",args:null},{Class:"span20",hover:"span20_hover",title:"自动排版",action:"Layout",args:null}];newEditer($('ss'), data, LightBox.init());
}</script>
</body
</html> 

学习js在线html(富文本)编辑器相关推荐

  1. js获取UEditor富文本编辑器的内容

    在实现新闻管理时用到了百度的UEditor编辑器,之前也用过,阅读文档然后操作是没出现任何问题的(使用form表单提交).现在是想用js获取UEditor内容然后用ajax实现异步提交,死活都get不 ...

  2. 富文本编辑器 —— 学习笔记

    富文本编辑器 -- 学习笔记 文章目录 富文本编辑器 -- 学习笔记 一.wangEditor 1.内容处理--获取/设置 JSON 一.wangEditor 1.内容处理--获取/设置 JSON 使 ...

  3. java实现在线富文本编辑器,并传格式数据给后端

    做了一个简易在线的富文本编辑器,前端是html,后台是Java 前端代码: <html lang="en" xmlns:th="http://www.thymele ...

  4. ckeditor富文本编辑器的使用和图片上传,复制粘贴图片上传

    项目开发需要用到在线编辑和图片上传,最终讨论使用ckeditor,原因就是其丰富的API.考虑到最新版本ckeditor5可能不够稳定,我们选择使用ckedtior4.9.2版本.官网链接:ckedi ...

  5. ckeditor富文本编辑器的使用和图片上传

    项目开发需要用到在线编辑和图片上传,最终讨论使用ckeditor,原因就是其丰富的API.考虑到最新版本ckeditor5可能不够稳定,我们选择使用ckedtior4.9.2版本.官网链接:ckedi ...

  6. 阿里云oss云存储图片上传在wangEditor富文本编辑器上的集成

    调用方式 html: <textarea name="newsinfo_content" id="editor" style="height:5 ...

  7. 帝国CMS TAGS 结合百度富文本编辑器

    先下载对应百度富文本编辑器文件(这就不提了 网上一大堆) 接下来将下载的文件放置/e/data/ecmseditor下 页面引入对应的js文件 <script src="../../d ...

  8. vue项目 vue-quill-editor富文本编辑器+图片上传

    目录 基础使用: 进阶版: 使用 el-upload 图片上传: 进阶版2.0: 可拖动调整图片大小: 基础使用: 富文本编辑器: 此方法得到的图片为base64编码,图片上传在下面. npm ins ...

  9. 结合element-ui实现文件上传以及富文本编辑器的使用

    一.配置文件上传两个注意事项 1.action属性必须是绝对路径 2.手动配置请求头headers 完整代码: <el-uploadaction="http://www.ysqorz. ...

最新文章

  1. java redis释放连接池_Java 使用连接池操作redis
  2. 解决mysql中limit和in不能同时使用的问题
  3. Android性能优化常见问题,终局之战
  4. 【数理知识】《矩阵论》方保镕老师-第3章-矩阵的分解
  5. BETA、RC、ALPHA、Release、GA 等版本号的意义
  6. Java参数传递笔记
  7. Mysql partition by
  8. UI设计实用干货素材|引导页模板
  9. 17.1 MySQL主从介绍;17.2 准备工作;17.3 配置主;17.4 配置从;17.5 测试主从同步...
  10. [转载] 机器学习模型的保存和调用
  11. Uni-G/ The University of Glasgow (Uni-G) ECG Analysis Program
  12. 为什么用共有属性来封装私有变量
  13. 冯诺依曼机器人_人工智能 | 从智能家电到机器人,从谷歌大脑到人类
  14. 2021-03-04
  15. 创造生命奇迹的幸存者
  16. 专业的WiFi检测工具有哪些?如何解决wifi信号不好?
  17. 世界各国Google域名后缀对照表
  18. linux代码丢了,Linux 使用ss命令代替 netstat
  19. FileZilla显示 连接超时 无法连接到服务器的解决方案
  20. NAS存储忘记密码如何解决?

热门文章

  1. 正念,探寻自我觉醒之路(深度想考)
  2. js-前端video播放器
  3. 配置docker 代理,连接外部网络并下载docker镜像
  4. 12306要满足春运难点在哪里?外包给阿里,IBM等大型企业可不可行?
  5. CF 704A Thor
  6. 动漫人物头发怎么画(头发画法步骤)
  7. 模块化高扩展性的前端框架 KISSY
  8. grafana mysql 时间段选择_mysql – 选择特定时间段内的所有可用项目
  9. linux服务器使用nginx部署静态网页(已自测)
  10. 华为交换机配置SSH——实现远程登录调试