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)