JavaScript 输出

84 篇文章 0 订阅

JavaScript 输出

很遗憾,在javascript中我们找不到输出和打印的函数。
但是我们可以使用其他的方式来显示需要的数据:

  • 我们可以使用 console.log() 在浏览器控制台上面输出内容
  • 我们可以使用innerHTML 把内容写入到 HTML 元素。
  • 我们可以使用 document.write() 方法将内容写到 HTML 文档中。
  • 我们可以使用 window.alert() 弹出警告框。

那么window.alert怎样使用呢?

我们可以使用 window.alert() 弹出警告框:

<!DOCTYPE html>
<html>
<body>
<h1>this is my first title</h1>
<p>this is my first content</p>

<script>
window.alert(7 * 8);
</script>

</body>
</html>

那么HTML中的 元素如何操作呢?

我们可以使用document.getElementById(id) 来查找html中的元素,我们使用id的属性来标识唯一的html元素,根据这些唯一的id我们可以找到这些元素,从而操作这些元素。
举例说明:

<!DOCTYPE html>
<html>
<body>

<h1>this is my web page</h1>

<p id="test">this is my content</p>

<script>
document.getElementById("test").innerHTML = "content has been changed";
</script>

</body>
</html>

那么如何将内容写到 HTML中呢?

举例说明:

<!DOCTYPE html>
<html>
<body>

<h1>this is my web page</h1>

<p>this is my content</p>

<script>
document.write(Date());
</script>

</body>
</html>

注意: 我们需要在文档加载完成之后执行document.write(), 否则整个页面的内容将被覆盖。

<!DOCTYPE html>
<html>
<body>

<h1>this is my web page</h1>

<p>this is my content</p>

<button onclick="test()">clickMe</button>

<script>
function test() {
    document.write(Date());
}
</script>

</body>
</html>

那么我们怎样把内容输出到控制台呢?

通常我们可以使用console.log() 将想要的内容输出到浏览器中,这边方便我们的调试。
浏览器中,我们可以使用快捷键F12来开启调试模式。

举例说明:

<!DOCTYPE html>
<html>
<body>

<h1>this is my wbe page</h1>

<script>
e = 5;
f = 6;
g = e + f;
console.log(g);
</script>

</body>
</html>

console运行结果:
在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三季人 G

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值