Friends who have just started to contact js must be exposed to the output of js, so they will definitely use document.write, alert, innerHTML, console.log. Here is a brief introduction. Friends in need can refer to it
JavaScript Output
JavaScript does not have any printing or output functions.
JavaScript displays data
JavaScript can output data in different ways:
Use window.alert()
Pop up an alert box.
Use the document.write()
method to write the content into the HTML document.
Use innerHTML
Write to HTML element.
Use console.log()
Write to the browser's console.
Use window.alert()
You can pop up an alert box to display data:
<!DOCTYPE html> <html> <body> <h1>我的第一个页面</h1> <p>我的第一个段落。</p> <script> window.alert(5 + 6); </script> </body> </html>
Manipulate HTML elements
such as To access an HTML element from JavaScript, you can use the document.getElementById(id) method.
Please use the "id" attribute to identify HTML elements, and innerHTML to get or insert element content:
<!DOCTYPE html> <html> <body> <h1>我的第一个 Web 页面</h1> <p id="demo">我的第一个段落</p> <script> document.getElementById("demo").innerHTML = "段落已修改。"; </script> </body> </html>
The above JavaScript statement (in the