如何在 JavaScript 中顯示輸出?
可以在 JavaScript 中以 4 種方式顯示輸出。
a) 使用 innerHTML 屬性在 HTML 元素中顯示輸出。
示例
<html>
<body>
<p id="display"></p>
<script>
document.getElementById("display").innerHTML = 10 + 10;
</script>
</body>
</html>輸出
20.
b) 使用 document.write() 顯示輸出。
示例
<html> <body> <script> document.write(2 + 3); </script> </body> </html>
輸出
5
c) 透過 console.log() 顯示輸出。
示例
<html> <body> <script> console.log(2 + 3); </script> </body> </html>
在偵錯程式控制檯中,輸出
5.
d) 透過警報框顯示輸出。
示例
<html> <body> <script> alert(2 + 3); </script> </body> </html>
在警報視窗框中,輸出
5
如果要處理 JSON 字串或其他一些大資料,那麼 console.log 是最佳選擇。
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP