JavaScript Output

JS can "display" data in different ways:

(1)Writing into an alert box, using window.alert();

1 <!DOCTYPE html>
2 <html>
3 <body>
4 <h1>My first example.</h1>
5 <script>
6 window.alert(5 * 6);
7 </script>
8 </body>
9 </html>
View Code

(2)Writing into the HTML output using document.write();

1 <script>
2 document.write(5 + 8);
3 </script>
View Code

But, using document.write() after an HTML document is fully loaded, will delete all existing HTML:

<!DOCTYPE html>
<html>
<body>

<h1>My First Web Page</h1>
<p>My first paragraph.</p>

<button οnclick="document.write(5 + 6)">Try it</button>

</body>
</html>
View Code

(3)Writing into an HTML element, using innerHTML;

 1 <!DOCTYPE html>
 2 <html>
 3 <body>
 4 
 5 <h1>My First Web Page</h1>
 6 <p>My First Paragraph.</p>
 7 
 8 <p id="demo"></p>
 9 
10 <script>
11 document.getElementById("demo").innerHTML = 5 + 6;
12 </script>
13 
14 </body>
15 </html>
View Code

(4)Writing into the browser console, using console.log().

 1 <!DOCTYPE html>
 2 <html>
 3 <body>
 4 
 5 <h1>My First Web Page</h1>
 6 <p>My first paragraph.</p>
 7 
 8 <p>
 9 Activate debugging in your browser (Chrome, IE, Firefox) with F12, and select "Console" in the debugger menu.
10 </p>
11 
12 <script>
13 console.log(5 + 6);
14 </script>
15 
16 </body>
17 </html>
View Code

这样就可以按F12调试自己的代码了。

转载于:https://www.cnblogs.com/Bonnieh/p/5656454.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值