Principles of online HTML editor (DEMO)

Source: Internet
Author: User
Why online editing? First, we need the support of IE. After IE 5.5, there will be an editing status, that is, using this editing status, and then using JavaScript to control online editing.
The following is a brief example:

First, there must be an editing box, which is actually an editable webpage. Here we use IFRAME to create an editing box.

<IFRAME id = htmledit style = "width: 100%; Height: 296px" marginwidth = 0 marginheight = 0>
</Iframe>

Add JavaScript code to specify that htmledit has the Edit function:

Function document. onreadystatechange ()
{
Htmledit.doc ument. designMode = "on ";
}

 

Htmledit.doc ument. Body. innerhtml can get the HTML code in htmedit. In general, we will use this JavaScript to pass the content in IFRAME to a textarea and then submit it to the server for processing.

Function getiframedata (){
Document.form1.test.value?htmledit.doc ument. Body. innerhtml;
}
Function sentiframedata (){
Htmledit.doc ument. Body. innerhtml = Document. form1.test. value;
}

VaR sel = htmledit.doc ument. selection. createRange (); this sentence can obtain the selected focus:

The following example shows how to complete the process. An online editor with the bold feature. If you are interested, you can complete other functions on this basis !!

<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Script language = "JavaScript">
Function getiframedata (){
Document.form1.test.value?htmledit.doc ument. Body. innerhtml;
}
Function sentiframedata (){
Htmledit.doc ument. Body. innerhtml = Document. form1.test. value;
}
Function DOB (){
Htmledit. Focus ();
VaR sel = htmledit.doc ument. selection. createRange ();
Inserthtml ("<B>" + SEL. Text + "</B> ");
}
Function inserthtml (HTML ){
If (htmledit.doc ument. selection. type. tolowercase ()! = "NONE "){
Htmledit.doc ument. selection. Clear ();
}
Htmledit.doc ument. selection. createRange (). pastehtml (HTML );
}
Function document. onreadystatechange ()
{
Htmledit.doc ument. designMode = "on ";
}
</SCRIPT>
</Head>

<Body>
<Form action = "test. asp? Act = Add "method =" Post "name =" form1 ">
 
<IFRAME id = htmledit style = "width: 100%; Height: 296px" marginwidth = 0 marginheight = 0>
</Iframe>
<Textarea name = "test" rows = "10" id = "test" style = "width: 100%;"> </textarea>
<Br>
<Input type = "Submit" name = "Submit" value = "Submit">
<Input type = "button" value = "iframe-> textarea" onclick = "getiframedata ()">
<Input type = "button" value = "textarea-> iframe" onclick = "sentiframedata ()">
<Input type = "button" value = "B" onclick = "Dob ()">
</Form>
</Body>
</Html>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.