当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 松软科技带你学前端:JavaScript 输出

松软科技带你学前端:JavaScript 输出

2019年09月30日  | 移动技术网IT编程  | 我要评论
JavaScript 不提供任何内建的打印或显示函数。 JavaScript 显示方案 JavaScript 能够以不同方式“显示”数据: 使用 window.alert() 写入警告框 使用 document.write() 写入 HTML 输出 使用 innerHTML 写入 HTML 元素 使 ...

javascript 不提供任何内建的打印或显示函数。

javascript 显示方案

javascript 能够以不同方式“显示”数据:

  • 使用 window.alert() 写入警告框
  • 使用 document.write() 写入 html 输出
  • 使用 innerhtml 写入 html 元素
  • 使用 console.log() 写入浏览器控制台

使用 innerhtml

如需访问 html 元素,javascript 可使用 document.getelementbyid(id) 方法。

id 属性定义 html 元素。innerhtml 属性定义 html 内容:

实例

<!doctype html>
<html>
<body>

<h1>我的第一张网页</h1>

<p>我的第一个段落</p>

<p id="demo"></p>

<script>
 document.getelementbyid("demo").innerhtml = 5 + 6;
</script>

</body>
</html> 

使用 document.write()

出于测试目的,使用 document.write() 比较方便:

<!doctype html>
<html>
<body>

<h1>我的第一张网页</h1>

<p>我的第一个段落</p>

<script>
document.write(5 + 6);
</script>

</body>
</html> 

使用 window.alert()

您能够使用警告框来显示数据:

实例

<!doctype html>
<html>
<body>

<h1>我的第一张网页</h1>

<p>我的第一个段落</p>

<script>
window.alert(5 + 6);
</script>

</body>
</html> 

使用 console.log()

在浏览器中,您可使用 console.log() 方法来显示数据。

请通过 f12 来激活浏览器控制台,并在菜单中选择“控制台”

<!doctype html>
<html>
<body>

<h1>我的第一张网页</h1>

<p>我的第一个段落</p>

<script>
console.log(5 + 6);
</script>

</body>
</html>

文章来源:松软科技(www.sysoft.net.cn)

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网