createTextNode and innerHTML differences and line breaks

Source: Internet
Author: User

today write code, use to createTextNode, found as if the function and innerHTML is the same, and then consulted the online information.

First, createTextNode for example:

var element = document.createelement ("div"); element.classname = "message"; var Textnode = document.createTextNode ("  <strong>Hello</strong>"); Element.appendchild (Textnode);d ocument.body.appendChild (element);

Ii. InnerHTML Example: results: <Strong>Hello</Strong>

<><ID= "H2"></H2 ></ Div > document.getElementById ("H2"). InnerHTML = " < Strong >Hello</strong>"; Result: Hello recognized as bold bold

Third, line-wrapping

var t = document.createtextnode ("Hello W\norld"), var t = document.createelement ("P"); t.innerhtml= "Hello W < BR />Orld ";

Iv. differences

innerHTML and createTextNode can add a piece of content to a node, except that if the content has HTML tags (such as <strong></strong> in the example), it will be different. It will be treated as text in createTextNode and will not be parsed by the browser, but will be treated as HTML code with innerHTML (as in your example, hello will be shown in bold).
In general, if you are sure that there is no HTML tag in the content you want to insert, you can use innerHTML, which is more concise, but it is recommended to use createTextNode if you are unsure (such as inserting user input).

createTextNode and innerHTML differences and line breaks

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.