Home >Web Front-end >JS Tutorial >What are the output methods of js? 4 ways to output content (code example)
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('php中文网') alert("网站:www.php.cn") </script>
Rendering:
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>
Rendering:
console.log();Prompt a warning in the console
console.warn();When an error occurs, the error will be displayed in the console
console.error();Code example:
<script type="text/javascript"> console.log('控制台.日志()'); console.warn('控制台.警告()'); console.error('控制台.错误()'); </script>Effect picture:
HTMLElementObject.innerHTML=textHTMLElementObject: node element that needs to output content text: content that needs to be output
<!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>Rendering:
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!