欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

03.js输出数据的几种方式

程序员文章站 2024-03-17 16:25:58
...

1.window,alert():弹出警告框

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    window.alert(1+1);
</script>
</body>
</html>

2.document.write()

将内容写到HTML中

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
   document.write("你好");
</script>
</body>
</html>

3.innerHTML

写入到HTML元素中

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<p id="demo">你好</p>
<script>
   document.getElementById("demo").innerHTML="修改完毕!";
</script>
</body>
</html>

4.console.log():写入到浏览器的控制台

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    a = 1;
    b = 2;
    c = a + b;
    console.log(c);
</script>
</body>
</html>
相关标签: JavaScript