JavaScript InnerHTML, outerHTML, innertext, outertext distinction _javascript techniques

Source: Internet
Author: User
Tags html form html tags

1, Feature description:
InnerHTML Sets or gets the HTML
outerhtml setting or getting an object and its contents in the beginning and end tags of an object
InnerText settings or Text
Outertext settings (including labels) or get (excluding labels) objects in the start and end tags of an object
2, example

 

3, the difference:
simply put, the difference between innerHTML and outerHTML, innertext and Outertext is:
1), innerHTML and outerhtml the HTML that is included in setting the object's content will be parsed, and innertext and Outertext will not.
2, when setting, innerHTML and innertext only set the text within the label, while outerHTML and Outertext set the text including the label.

Special Note:
innerHTML is an attribute that conforms to the standard of the web, and innertext only applies to IE, 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, and here is a simple example that meets the requirements of the international Standards of the Consortium:

<a href= "Javascript:alert (document.getElementById (' Test '). Innerhtml.replace (/<.+?>/gim, ')" > No HTML, Conforming to the </a> of the International Consortium

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.