PK of innerHTML and innertext

Source: Internet
Author: User

The InnerText property is used to define the text to be output by the object, in this case innertext the text in the object it "do you like to see a smile very much?" "Become" Super like! "(Statement it.innertext=" Super like!) ")。 The change to the object IH with the innerHTML attribute, in addition to the role of innertext, it can also change the object IH inside the HTML statement, so it changed the text to "Chonay!", and the text output changed to italic (<i></i>) and Bold ( <b></b>), which is the statement ih.innerhtml= "<i><b> Chonay!</b></i>".

<! DOCTYPE HTML Public"-//w3c//itd HTML 4.01 transitional//en"        "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.ITD">"UTF-8"> <title>innertext and innerHTML usage instances </title>"color:red;"> Click on the text below ......</span><ul> <li id="it"onclick="Changetext ()"> Would you like to see a little smile? </li> <li id="IH"onclick="changehtml ()"> What's your name? </li> <li onclick="Back ()"> Back to the original! </li></ul></body><script type="Text/javascript">function Changetext () {It.innertext="Super Love!"; }//functionfunction changehtml () {ih.innerhtml="<i><b> Chonay!</b></i>"; }//functionfunction Back () {It.innertext="Would you like to see a little smile ?"; Ih.innerhtml="What's your name?"; }</script>

Second, the effect of the following example is to first enter text in the text box, and then press the "Output text" button, and then the page will automatically output the text you entered

<! DOCTYPE HTML Public"-//w3c//itd HTML 4.01 transitional//en"        "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.ITD">"UTF-8"> <title> making Dynamic Web </title>"frm"> <p> <span style="color:red;"> enter text in a text box:</span><br/><br/> <label> first text box: </label><input type="text"Name="txt"Size=" -"/><br/><br/> <label> Second text box:</label> <input type="text"Name="txt2"Size=" -"/> <br><br/> <input type="Button"Value="Output Text"Name="B1"style="Color:blue;"onclick="Outputtext ()"> </p></form><p id="Output">to output text here:</p></body><script type="Text/javascript">function Outputtext () {if(frm.txt2.text!="") {output.innerhtml="output text here:<b>"+frm.txt2.value+"</b>";}//output is an object.         Else{Output.innertext="to output text here:";}//if (frm.txt2!= "") {//output.innerhtml= "Output text here:<b>" +frm.txt2.value+ "</b>";} //output is an object. //else {//output.innertext= "Output text here:";}//if (frm.txt2.value!= "") {//output.innerhtml= "Output text here:<b>" +frm.txt2.value+ "</b>";} //output is an object. //else {//output.innertext= "Output text here:";}    }</script>

Summarize:

InnerText and innerHTML get the contents of the element. If the element contains only text, then innertext and innerHTML return the same value. However, if both text and other elements are included, InnerText will return only the representation of the text, and innerHTML will return all the elements and the HTML code of the text. The following table lists the different values that are returned based on the specific code innertext and innerHTML.

Code InnerText InnerHTML
<div>hello world</div> "Hello World" "Hello World"
<div><b>Hello</b>world</div> "Hello World" "<b>hello</b>world"
<div><span></span></div> "" "<span></span>"


Finally, by assigning the innertext to itself, it means removing all HTML tags from the specified element.
Odiv.innertext = Odiv.innertext;

Supplement: InnerText, InnerHTML, Outtext, outhtml Ultimate PK

First, Function Introduction:

InnerHTML Sets or gets the HTML that is located within the start and end tags of the object
outerHTML Sets or gets the HTML form of an object and its contents
InnerText set or get text that is within the start and end tags of an object
Outertext set (including tags) or get (excluding tags) the text of an object

Ii. Examples of reference

<! DOCTYPE HTML Public"-//w3c//itd HTML 4.01 transitional//en"        "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.ITD">"UTF-8"><title>innertext, InnerHTML, Outtext, Outhtml</title>"Color:lawngreen;"> Please click on the list below to change the body:</p><ul> <li id="Test_id1"onclick="Innerhtmldemo ()">innerhtml effect .</li> <li id="Test_id2"onclick="Innertextdemo ()">innertext effect .</li> <li id="TEST_ID3"onclick="Outerhtmldemo ()">outerhtml effect .</li> <li id="Test_id4"onclick="Outertextdemo ()">outertext effects .</li> </ul></body> <script language="JavaScript">//. InnerHTMLfunction Innerhtmldemo () {test_id1.innerhtml="<i><b> set or get html.</b></i> in the start and end tags of objects"; }    //. InnerTextfunction Innertextdemo () {Test_id2.innertext="<i><b> Sets or gets the text that is within the start and end tags of the object .</b></i>"; }    //. outerHTMLfunction Outerhtmldemo () {test_id3.outerhtml="<i><b> Sets or gets the HTML form of the object and its contents .</b></i>"; }    //. Outertextfunction Outertextdemo () {Test_id4.outertext="<i><b> set (including tags) or get (excluding tags) the text of an object .</b></i>"; }    </script> 

Three, the difference:
The difference between innerHTML and outerHTML, innertext and Outertext is simply that:
1), innerHTML, and outerhtml the HTML that is included when setting the content of the object is parsed, and innertext and outertext do not.
2), when set, innerHTML and innertext only set the text within the label, while outerHTML and Outertext set the text including the label.

Special Note:
innerHTML is a standard-compliant attribute, and innertext only works with Internet Explorer, so use innerHTML as much as possible, and less innertext, if you want to output content that does not contain HTML tags, You can use innerHTML to get content that contains HTML tags, and then use regular expressions to remove HTML tags.

PK of innerHTML and innertext

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.