JavaScript does not have any printing or output functions. In HTML, JavaScript is usually used to operate HTML elements.
JavaScript output
JavaScript does not have any printing or output functions.
In HTML, JavaScript is usually used to operate HTML elements.
Operate HTML elements
To access an HTML element from JavaScript, you can use the document. getElementById (id) method.
Use the "id" attribute to identify HTML elements and innerHTML to obtain or insert element content:
Instance
My first Web pageMy first paragraph
Script document. getElementById ("demo"). innerHTML = "the section has been modified. "; Script
The preceding JavaScript statement (in the script tag) can be executed in a web browser:
Document. getElementById ("demo") is the JavaScript code that uses the id attribute to search for HTML elements.
InnerHTML = "Paragraph changed." Is the JavaScript code used to modify the HTML content (innerHTML) of an element.
In this tutorial
In most cases, in this tutorial, we will use the method described above to output:
In the following example
Elements are written to HTML document output:
Write to HTML document
For testing purposes, you can write JavaScript directly in HTML documents:
Instance
My first Web pageMy first paragraph.
Script document. write (Date (); script
Use document. write () to output only the written content to the document.
If you execute document. write after the document has been loaded, the entire HTML page will be overwritten.
Instance
My first Web pageMy first paragraph.
Click meScript function myFunction () {document. write (Date ();} script
Write to console
If your browser supports debugging, you can use console. log () to display JavaScript values in your browser.
Use F12 in the browser to enable the debugging mode. In the debugging window, click the "Console" menu.
Instance
My first Web page script a = 5; B = 6; c = a + B; console. log (c); script
Do you know?
Debugging in the program is the process of testing, finding and reducing bugs (errors.
The above is the content output by JavaScript in the [JavaScript tutorial]. For more information, see the PHP Chinese website (www.php1.cn )!