用Javascript 编写 HTML在线编辑器

以前一直不知道好多网站上所说的在线编辑器是怎么回事,后来在文档里发现document 对象的一个方法。

document.execCommand(command, false, value);
才知道具体原理。

一、首先来看一个例子:

<DIV contenteditable="true" style="border:dashed blue 2px">Hello World!</DIV>

保存为html网页,打开看看,在DIV里出现了一个光标,这个DIV就变成可以编辑的了。

类似的,SPAN,FONT等都可以有 contenteditable="true"  这个属性。

再试试下面的:

<DIV contenteditable="true" style="border:dashed blue 2px">Hello World!
    <IMG src="https://p-blog.csdn.net/images/p_blog_csdn_net/comstep/70786/o_logo.jpg" />
</DIV>

我们就可以拉伸图片了。

二、具体实现:

    1、需要两个页面,blank.html editor.html

    2、blank.html 作为 editor.html的一个内嵌Frame,作为编辑框。

<html>
<body topmargin="10" leftmargin="10" bgColor="#f6f6f6">
  <div id="RTC" contenteditable = true></div>
</body>
</html>

    3、editor.html 主要是一些Javascript,用来处理不同的命令。

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<SCRIPT LANGUAGE="JavaScript">
<!--
var contentHTML;
function exeCommand(command, value)
{
  document.execCommand(command, false, value);
}

// 加粗
function Black()
{
  var obj = frames['ifRTC'].RTC;
  obj.focus();
  exeCommand('Bold', '');
}

// 斜体
function Italic()
{
  var obj = frames['ifRTC'].RTC;
  obj.focus();
  exeCommand('Italic', '');
}

// 下划线
function UnderLine()
{
  var obj = frames['ifRTC'].RTC;
  obj.focus();
  exeCommand('Underline', '');
}

// 向里缩进
function Indent()
{
  var obj = frames['ifRTC'].RTC;
  obj.focus();
  exeCommand('Indent', '');
}

// 向外缩进
function Outdent()
{
  var obj = frames['ifRTC'].RTC;
  obj.focus();
  exeCommand('Outdent', '');
}

// 无序列表
function UnorderList()
{
  var obj = frames['ifRTC'].RTC;
  obj.focus();
  exeCommand('InsertUnorderedList', '');
}

// 有序列表
function OrderList()
{
  var obj = frames['ifRTC'].RTC;
  obj.focus();
  exeCommand('InsertOrderedList', '');
}

// 插入图片
function Image()
{
  var obj = frames['ifRTC'].RTC;
  obj.focus();
  ImagePath = window.prompt('请输入图片路径:', '');
  exeCommand('InsertImage', ImagePath);
}

// 预览效果
function Preview()
{
  var htmlContent = frames['ifRTC'].RTC.innerHTML;
  var open = window.open('');
  open.document.write(htmlContent);
}

// 查看编辑框里的HTML源代码
function Source()
{
  var htmlContent = frames['ifRTC'].RTC.innerHTML;
  if (document.all.iframeDiv.style.display == 'block')
  {
    document.all.iframeDiv.style.display = 'none';
    document.all.htmlText.value = htmlContent;
    document.all.textDiv.style.display = 'block';
    document.all.htmlText.focus();
    document.all.Source.value='HTML';
  }
  else
  {
    document.all.iframeDiv.style.display = 'block';
    document.all.textDiv.style.display = 'none';
    frames['ifRTC'].RTC.innerHTML = document.all.htmlText.value;
    frames['ifRTC'].RTC.focus();
    document.all.Source.value=' 源代码 ';
  }
}

// 增加编辑框的高度
function Add()
{
  document.all.ifRTC.height = document.all.ifRTC.height*1 + 50;
}
//-->
</SCRIPT>
</HEAD>

<BODY>
<TABLE width="400"border="0">
<TR>
 <TD><input type="button" value="B" name="Black" οnclick="Black()" /></TD>
 <TD><input type="button" value="I" name="Italic" οnclick="Italic()" /></TD>
 <TD><input type="button" value="U" name="UnderLine" οnclick="UnderLine()" /></TD>
  <TD><input type="button" value="UL" name="UnorderList" οnclick="UnorderList()" /></TD>
  <TD><input type="button" value="OL" name="OrderList" οnclick="OrderList()" /></TD>
 <TD><input type="button" value="左" name="Outdent" οnclick="Outdent()" /></TD>
 <TD><input type="button" value="右" name="Indent" οnclick="Indent()" /></TD>
 <TD><input type="button" value="图" name="Image" οnclick="Image()" /></TD>
</TR>
</TABLE>
<div id="iframeDiv" style="display:block">
<iframe id="ifRTC" width="400" height="200" border="1" src="blank.html" ></iframe>
</div>
<div id="textDiv" style="display:none">
  <textarea id="htmlText" cols="50" rows="10"></textarea>
</div>
<br>
<input type="button" value=" + " name="Add" οnclick="Add()" />&nbsp;&nbsp;
<input type="button" value=" 预  览 " name="Preview" οnclick="Preview()" />&nbsp;&nbsp;
<input type="button" value=" 源代码 " name="Source" οnclick="Source()" />
</BODY>
</HTML>

src="http://www.fadesky.com/360doc.html" frameborder="0" width="380" scrolling="no" height="300">

三、后记:

这里写的只是一个简单的编辑器,其实重要的就是:

contenteditable="true"

document.execCommand(command, false, value);
关于 document 的一些方法,可以查看MS的文档。
execCommand 的一些命令也可以在文档里找到,下面列出一些:

execCommand(command, false, value); 中的 command 可以是以下这些:

BackColorSets or retrieves the background color of the current selection.
BoldToggles the current selection between bold and nonbold.
ClearAutocompleteForFormsClears saved forms data.
CopyCopies the current selection to the clipboard.
CreateBookmarkRetrieves the name of a bookmark anchor or creates a bookmark anchor for the current selection or insertion point.
CreateLinkRetrieves the URL of a hyperlink or creates a hyperlink on the current selection.
CutCopies the current selection to the clipboard and then deletes it.
DeleteDeletes the current selection.
FontNameSets or retrieves the font for the current selection.
FontSizeSets or retrieves the font size for the current selection.
ForeColorSets or retrieves the foreground (text) color of the current selection.
FormatBlockSets or retrieves the current block format tag.
IndentIncreases the indent of the selected text by one indentation increment.
InsertButtonOverwrites a button control on the current selection.
InsertFieldsetOverwrites a box on the current selection.
InsertHorizontalRuleOverwrites a horizontal line on the current selection.
InsertIFrameOverwrites an inline frame on the current selection.
InsertImageOverwrites an image on the current selection.
InsertInputButtonOverwrites a button control on the current selection.
InsertInputCheckboxOverwrites a check box control on the current selection.
InsertInputFileUploadOverwrites a file upload control on the current selection.
InsertInputHiddenInserts a hidden control on the current selection.
InsertInputImageOverwrites an image control on the current selection.
InsertInputPasswordOverwrites a password control on the current selection.
InsertInputRadioOverwrites a radio control on the current selection.
InsertInputResetOverwrites a reset control on the current selection.
InsertInputSubmitOverwrites a submit control on the current selection.
InsertInputTextOverwrites a text control on the current selection.
InsertMarqueeOverwrites an empty marquee on the current selection.
InsertOrderedListToggles the current selection between an ordered list and a normal format block.
InsertParagraphOverwrites a line break on the current selection.
InsertSelectDropdownOverwrites a drop-down selection control on the current selection.
InsertSelectListboxOverwrites a list box selection control on the current selection.
InsertTextAreaOverwrites a multiline text input control on the current selection.
InsertUnorderedListToggles the current selection between an ordered list and a normal format block.
ItalicToggles the current selection between italic and nonitalic.
JustifyCenterCenters the format block in which the current selection is located.
JustifyLeftLeft-justifies the format block in which the current selection is located.
JustifyRightRight-justifies the format block in which the current selection is located.
OutdentDecreases by one increment the indentation of the format block in which the current selection is located.
OverWriteToggles the text-entry mode between insert and overwrite.
PasteOverwrites the contents of the clipboard on the current selection.
RefreshRefreshes the current document.
RemoveFormatRemoves the formatting tags from the current selection.
SelectAllSelects the entire document.
SaveAsSaves the current Web page to a file.
UnBookmarkRemoves any bookmark from the current selection.
UnderlineToggles the current selection between underlined and not underlined.
UnlinkRemoves any hyperlink from the current selection.
UnselectClears the current selection.
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值