富文本编辑器,Rich Text Editor, 简称 RTE, 是一种可内嵌于浏览器,所见即所得的文本编辑器。
页面中嵌入一个包含空HTML页面的iframe。设置designMode属性,使空白页面HTML的body可以被编辑。
designMode:off/on
* 页面加载完才可以设置designMode属性,所以需要使用onload事件。
<iframe name="demoDesignMode" src="demoDesignMode.html" style="height: 100px;width: 100px;"></iframe>
<script>
window.onload= function(){
frames['demoDesignMode'].document.designMode = 'on';
}
</script>
contenteditable='true'设置元素和它包含的元素可以编辑。
* 区分contenteditable和contentEditable。contenteditable是元素的特性,而contentEditable是对象的属性。
<div id="box" style="height: 100px;width: 100px; border:1px solid black"></div>
<button id="btn1">打开富文本编辑</button>
<button id="btn2">关闭富文本编辑</button>
<script>
btn1.onclick = function(){box.contentEditable = true;}
btn2.onclick = function(){box.contentEditable = false;}
</script>
document.execCommand()对文档执行预定义的命令。
document.execCommand(String aCommandName, Boolean aShowDefaultUI, String aValueArgument):
一个 DOMString ,命令的名称。可用命令列表请参阅 命令 。
一个 Boolean, 是否展示用户界面,一般为 false。Mozilla 没有实现。
一些命令(例如insertImage)需要额外的参数(insertImage需要提供插入image的url),默认为null。
* 因为浏览器兼容性问题,第二个参数要为false,firefox在该参数为true时抛出错误。
居中 document.execCommand('justifyCenter');
左对齐 document.execCommand('justifyLeft');
右对齐 document.execCommand('justifyRight');
添加缩进 document.execCommand('indent');
去掉缩进 document.execCommand('outdent');
字体类型 document.execCommand('fontname',false,sFontName)
字体大小 document.execCommand('fontsize',false,sFontSize)
字体颜色 document.execCommand('forecolor',false,sFontColor)
背景色 document.execCommand('backColor',false,sBackColor)
加粗 document.execCommand('bold');
斜体 document.execCommand('italic');
下划线 document.execCommand('underline');
复制 document.execCommand('copy');
剪切 document.execCommand('cut');
粘贴 document.execCommand('paste');(经测试无效)
全选 document.execCommand('selectAll');
删除 document.execCommand('delete');
后删除 document.execCommand('forwarddelete');
清空格式 document.execCommand('removeFormat');
前进一步 document.execCommand('redo');
后退一步 document.execCommand('undo');
打印 document.execCommand('print');(对firefox无效)
插入标签 document.execCommand('formatblock',false,elementName);
插入<hr>
document.execCommand('inserthorizontalrule');
插入<ol>
document.execCommand('insertorderedlist');
插入<ul>
document.execCommand('insertunorderedlist');
插入<p>
document.execCommand('insertparagraph');
插入图像 document.execCommand('insertimage',false,URL);
增加链接 document.execCommand('createlink',false,URL);
删除链接 document.execCommand('unlink');
* IE8-浏览器不支持,我试了IE8以上也不支持
富文本编辑不是使用表单控件实现的,而需要手工来提取并提交HTML。为此,通常可以添加一个隐藏的表单字段,就是在提交表单之前提取出HTML,并将其插入到隐藏的字段中。
form.onsubmit = function(e){
e = e || event;
var target = e.target || e.srcElement;
target.elements["comments"].value = frames["richedit"].document.body.innerHTML;
}
form.onsubmit = function(e){
e = e || event;
var target = e.target || e.srcElement;
target.elements["comments"].value = document.getElementById('wysiwyg').innerHTML;
}
<div id="editor-box">
<div id="editor-icon">
<div class="icon-box">
<span class="glyphicon glyphicon-bold" data-role="bold">加粗</span>
<span class="glyphicon glyphicon-italic" data-role="italic">斜体</span>
<span class="glyphicon glyphicon-align-center" data-role="justifyCenter">居中</span>
<span class="glyphicon glyphicon-align-left" data-role="justifyLeft">左对齐</span>
<span class="glyphicon glyphicon-align-right" data-role="justifyRight">右对齐</span>
<span class="glyphicon" data-role="indent">添加缩进</span>
<span class="glyphicon" data-role="outdent">去掉缩进</span>
<span class="glyphicon" data-role="h1">h1</span>
<span class="glyphicon" data-role="h2">h2</span>
<span class="glyphicon" data-role="p">p</span>
</div>
</div>
<div id="editor" contenteditable="true"> </div>
</div>
$(document).ready(function(e) {
$('.icon-box span').click(function(e) { $('#editor').focus();
getC($('#editor'))
switch($(this).data('role')) {
case 'h1':
case 'h2':
case 'p':
document.execCommand('FormatBlock', false, '<' + $(this).data('role') + '>');
console.log($(this).data('role'));
break;
default:
document.execCommand($(this).data('role'), false, null);
break;
}
})
}); function getC(that){
if(document.all){
that.range=document.selection.createRange();
that.range.select();
that.range.moveStart("character",-1);
}else{
that.range=window.getSelection().getRangeAt(0);
that.range.setStart(that.range.startContainer, that.range.startContainer.length);
}
}
实现一个富文本编辑器没有想的那么容易哦,这个只是简单的实践,实际是一个大工程哦。请看这个文章:
https://www.zhihu.com/question/38699645?sort=created
推荐几个编辑器:
widgEditor
wangeditor
ueditor