Home > Web Front-end > JS Tutorial > What are the output methods of js? 4 ways to output content (code example)

What are the output methods of js? 4 ways to output content (code example)

青灯夜游
Release: 2018-10-29 16:08:00
Original
13679 people have browsed it

How does JavaScript realize the output of content? What are the output methods of js? This article will introduce you to the method of outputting content in js, and let you know the four output statements of js. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Method 1: js window.alert(); statement output

window.alert() statement: you can output a warning box, and window can Omit. It has a pop-up effect, but because the pop-up is sudden, the user experience is not good. It is basically used for testing code.

Code example:

<script type="text/javascript"> 
    window.alert(&#39;php中文网&#39;)
    alert("网站:m.sbmmt.com")
</script>
Copy after login

Rendering:

What are the output methods of js? 4 ways to output content (code example)

Description:

window.alert() can change the small The content in brackets is displayed in the form of a pop-up window. This content needs to be quoted with a pair of single quotes or a pair of double quotes;

Because window is a BOM object, which refers to the entire browser, it can be omitted. Do not write.

Method 2: js document.write() statement output

document.write() method: you can write the content directly to the html document , output on the page.

Code example:

<script type="text/javascript"> 
    document.write("hello");
    document.write("<h1>通过document.write输出内容</h1>");
</script>
Copy after login

Rendering:

What are the output methods of js? 4 ways to output content (code example)

##Method 3: js console series statement output

The console series of statements can write the content that needs to be output to the browser's console (usually use the F12 key to open and view), and output the content in the console, which is often used during development.

Display the log in the console

console.log();
Copy after login

Prompt a warning in the console


console.warn();
Copy after login

When an error occurs, the error will be displayed in the console

console.error();
Copy after login

Code example:

<script type="text/javascript">
    console.log(&#39;控制台.日志()&#39;);
    console.warn(&#39;控制台.警告()&#39;);
    console.error(&#39;控制台.错误()&#39;);
</script>
Copy after login

Effect picture:


What are the output methods of js? 4 ways to output content (code example)

Method 4: js innerHTML statement output

innerHTML attribute : You can set or return the HTML between the opening and closing tags of the table row.

Basic syntax:

HTMLElementObject.innerHTML=text
Copy after login

HTMLElementObject: node element that needs to output content

text: content that needs to be output


Code example:


<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">
<title>通过innerHTML方法向页面元素中输出内容</title>
</head>
<script>
function changeContent() {
document.getElementById("demo").innerHTML = "通过innerHTML方法向页面输出了内容";
}
</script>

<body>
<h1 id="demo">我是一个标题</h1>
<button type="button" onclick="changeContent()">更改内容</button>
</body>

</html>
Copy after login
Rendering:


What are the output methods of js? 4 ways to output content (code example)

Summary: The above are the four js output methods introduced in this article, each with its own characteristics You can try it yourself to deepen your understanding and use different output methods according to different situations or needs. I hope it will be helpful to everyone's learning. For more related tutorials, please visit

JavaScript Video Tutorial, jQuery Video Tutorial, bootstrap Tutorial!

The above is the detailed content of What are the output methods of js? 4 ways to output content (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
js
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template