Use Javascript to change HTML content

Source: Internet
Author: User

Address: http://www.qeefee.com/js/change-html-used-javascript

For more Javascript tutorials, visit> Javascript tutorials

Body:

Javascript is usually used to manipulate HTML and Change Webpage content!

Output content to the page

Javascript provides the document. write ('string') method to write content to the page:

<! DOCTYPE html> 

You can copy this code to an html file and run it. When the page runs in a browser

At this time, we click the button and you will find that all the content on the page is overwritten:

The "culprit" of page content overwriting is the writeHtmlLater method called by the button onclick event. This method also writes a sentence to the page, but overwrites the whole page. Note the following:

Document. write () only outputs the written content to the document. If the document. write is executed after the document has been loaded, the entire HTML page will be overwritten.

Change HTML Tag content

We use JS not only to output some content to the document, but also to change the display of existing labels. The document tag access method is provided in Javascript: document. getElementById (). You can use this method to obtain the HTML tags you want to operate on and change their display:

<! DOCTYPE html> 

Run this code and you will see the following page content:

After you click the change Span content button, you will find that the content of the span tag has changed:

This is because we have obtained this tag in the Code and re-assigned the value to its innerHTML attribute.

This article briefly introduces two ways to change the HTML content. In the subsequent sections, I will introduce more Javascript knowledge. Please stay tuned!

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.