The difference between InnerHTML, innertext and outerHTML, outertext

Source: Internet
Author: User
Tags html form

Original: http://walsh.iteye.com/blog/261966

InnerHTML, innertext and outerhtml, outertext Difference Blog classification:

    • Css/html
Html 1. The difference is described as follows:
    • 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
InnerText and outertext are the same effect when they are acquired, but when set, innertext only sets the text within the label, and the outertext sets the text including the label.
2. Sample CodeHTML code
  1. <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd">   
  2. < HTML >   
  3. < Head >   
  4. < Meta http-equiv="Content-type" Content= "text/html; Charset=utf-8 ">
  5. < title > the difference between InnerHTML, outerhtml and innertext, outerHTML </ title >   
  6. < Script language="JavaScript" type=" Text/javascript ">
  7. . InnerHTML
  8. function Innerhtmldemo ()
  9. {
  10.    test_id1.innerhtml = <i><u> set or get html.</u></i> in the start and end tags of the object   ;
  11. }
  12. . innerText
  13. function Innertextdemo ()
  14. {
  15.    Test_id2.innertext = <i><u> Sets or gets the text that is within the start and end tags of the object .</u></i>   ;
  16. }
  17. . outerhtml
  18. function Outerhtmldemo ()
  19. {
  20.    test_id3.outerhtml = "<font size=9pt color=red><i><u> Sets or gets the HTML form of the object and its contents .</u></i></font>"   ;
  21. }
  22. . outertext
  23. function Outertextdemo ()
  24. {
  25.    Test_id4.outertext = "<br></br><i><u> set (including tags) or get (excluding tags) the text of the object .</u></i>"   ;
  26. }
  27.   </ Script >    
  28.   </ Head >    
  29.   < Body >    
  30.   < ul >    
  31. < li   id = "TEST_ID1"   onclick = "Innerhtmldemo ()" > The innerHTML effect. </ li >    
  32. < li   id = "Test_id2"   onclick = "Innertextdemo ()" > The innertext effect. </ li >    
  33. < li   id = "TEST_ID3"   onclick = "Outerhtmldemo ()" > The outerhtml effect. </ li >    
  34.   < Li id="Test_id4" onclick="Outertextdemo ()" >outertext effect. </ Li >    
  35.   </ ul >    
  36.   </ Body >    
  37.   </ HTML >   
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > 3, the difference:In a nutshell, the difference between innerHTML and outerHTML, innertext and Outertext is that: 1), innerHTML, and outerhtml the HTML that is included in setting the object's content is parsed.    and innertext and outertext are not.     2), when set, innerHTML and innertext only set the text within the label, while outerHTML and Outertext set the text including the label. For a DIV with an ID of "Testdiv", the difference between outerhtml, innerHTML, and innertext can be demonstrated by:
Related Article

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.