Home > Web Front-end > JS Tutorial > JavaScript output display content (basic tutorial)

JavaScript output display content (basic tutorial)

亚连
Release: 2018-05-19 14:04:26
Original
2104 people have browsed it

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>
Copy after login

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>
Copy after login

The above JavaScript statement (in the

Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template