javascript怎么输出数据?
有四种方式。
方式1:使用 window.alert() 弹出警告框(重点)。
方式2:使用 innerHTML 写入到 HTML 元素(重点)。
方式3:使用 document.write() 方法将内容写到 HTML 文档中。
方式4:使用 console.log() 写入到浏览器的控制台。
window.alert()演示(重点)
index.html代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<p>window.alert()</p>
<script>
window.alert(122);
</script>
</body>
</html>
展示图:
innerHTML演示
首先使用document.getElementById(id)的方法访问 HTML 元素。
使用 "id" 属性标识 HTML 元素,使用innerHTML 插入元素内容。
该命令也是可以在命令行输入的。
index.html代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<p>innerHTML</p>
<p id="demo">被修改的内容</p>
<script>
document.getElementById("demo").innerHTML = "修改内容";
</script>
</body>
</html>
展示图
document.write()演示
如果在文档已完成加载后执行 document.write,整个 HTML 页面将被覆盖。
index.html
<!DOCTYPE html>
<html>
<body>
<p>document.write()</p>
<script>
document.write(122);
</script>
</body>
</html>
console.log() 演示
index.html
<!DOCTYPE html>
<html>
<body>
<h1> console.log() </h1>
<script>
console.log(122);
</script>
</body>
</html>
演示图
以上就是javascript输出数据的四种方式。
喜欢的可以点赞或收藏,谢谢!