Document. Write and getelementbyid (ID)

Source: Internet
Author: User

In JavaScript, the document. Write () method is often used to output content from a webpage to a document.

Example: The document. Write () method is used to output a piece of text to the webpage document.

Document. Write ("I love learning-I like learning. ");

In addition, you can use the document. Write () method to output data in another way.

Document. Write () is output as a variable.

First, declare a variable.

VaR STR = "Hello World ";

Document. Write (STR );

Second, we can use the document. Write () method to output the combination of variables and strings, and concatenate the strings to achieve the desired output effect.

Note:

Concatenate a string with the plus sign "+ ";

The string is enclosed in double quotation marks.

Example:

VaR STR = "Hello World ";

Document. Write (STR + "" + "Hello World ");

// The output result in the browser is: Hello World

Finally, we can use the document. Write () method to output HTML tags. At the same time, we can write CSS styles into tags. Pay attention to the writing format and escape between quotation marks.

 

Document. getelementbyid (ID) // gets the object with the specified id value and returns a reference to the first object with the specified ID

In an HTML document, you can set the ID and name attributes for each element. The ID attribute is a unique attribute and cannot be repeated. An ID value can correspond to only one element, while the name attribute can be repeated. A name value can correspond to a group of elements. Therefore, only one selected HTML element is returned when document. getelementbyid (ID) is used;

Note:
Getelementbyid is used to set/return the attributes of HTML tags and call their events and methods.
Using this method, you can basically control all the tags on the page. The condition is very simple, that is, assign an ID number to each tag.
Returns a reference to the first object with the specified ID attribute value.

Syntax:
 

Element = Document. getelementbyidx_x_x ("ID ")

Obtain all attributes and methods of ID = ID
In JS, it is often used to obtain element values through the control ID. For example, a form contains text, label, and so on. They are all form elements, with an assigned ID, getelementbyid () is to obtain the text value of these elements.

 

Document usage

 

Document. Write and getelementbyid (ID)

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.