The online editor is very useful in our daily project development (such as news system), it can easily achieve the online editing of the article, eliminating the FrontPage and other tools. So how to achieve the browser online editing function? First of all need IE support, after the IE5.5 has an editing status. is to use this editing state and then use JavaScript to control online editing.
The first thing to have is an edit box, which is actually an editable page, and we use an IFRAME to create the edit box.
<iframe id= "Htmledit" style= "width:100%; height:296px "marginwidth=" 0 "marginheight=" 0 "></IFRAME>
and add JavaScript code to specify that Htmledit has an editing function (the complete original code is provided below):
Copy Code code as follows:
<script language= "JavaScript" >
var editor;
Editor = document.getElementById ("Htmledit"). Contentwindow;
Simply type the following setting, and the IFrame immediately becomes an editor.
Editor.document.designMode = ' on ';
Editor.document.contentEditable = true;
But IE is a little different from Firefox, so you have to create a new document to be compatible with Firefox.
Editor.document.open ();
Editor.document.writeln (' Editor.document.close ();
Font Effects-Bold method one
function Addbold ()
{
Editor.focus ();
All font effects can be done using Execcomman () only.
Editor.document.execCommand ("Bold", false, NULL);
}
Font Effects-Bold Method two
function Addbold ()
{
Editor.focus ();
Get the selected focus
var sel = Editor.document.selection.createRange ();
Inserthtml ("<b>" +sel.text+ "</b>");
}
function inserthtml (HTML)
{
if (Editor.document.selection.type.toLowerCase ()!= "None")
{
Editor.document.selection.clear ();
}
Editor.document.selection.createRange (). pastehtml (HTML);
}
</script>