JavaScript簡介
有一張圖片小小介紹吧拐纱,想要詳細了解點擊:JavaSCript 百科
JavaScript簡介.png
JavaScript輸出
JavaScript 沒有任何打印或者輸出的函數(shù)铜异。
JavaScript 顯示數(shù)據(jù)
JavaScript 可以通過不同的方式來輸出數(shù)據(jù):
- 使用 window.alert() 彈出警告框。
- 使用 document.write() 方法將內(nèi)容寫到 HTML 文檔中秸架。
- 使用 innerHTML 寫入到 HTML 元素揍庄。
- 使用 console.log() 寫入到瀏覽器的控制臺。
使用window.alert()
你可以彈出警告框來顯示數(shù)據(jù):
實例
><!DOCTYPE html>
<html>
<body>
><h1>我的第一個頁面</h1>
<p>我的第一個段落东抹。</p>
><script>
window.alert(5 + 6);
</script>
></body>
</html>```
[編輯運行](http://www.runoob.com/try/tryit.php?filename=tryjs_output_alert)
####***操作 HTML 元素***
________
如需從 JavaScript 訪問某個 HTML 元素蚂子,您可以使用```document.getElementById(id) ```方法。
**id**屬性標識 HTML 元素缭黔, **innerHTML** 獲取或插入元素內(nèi)容:
>**實例**
<!DOCTYPE html>
<html>
<body>
<h1>我的第一個 Web 頁面</h1>
<p id="demo">我的第一個段落</p>
<script>
document.getElementById("demo").innerHTML = "段落已修改食茎。";
</script>
</body>
</html>
[編輯運行](http://www.runoob.com/try/try.php?filename=tryjs_console)
####***寫到 HTML 文檔***
___________________
>**實例**
<!DOCTYPE html>
<html>
<body>
<h1>我的第一個 Web 頁面</h1>
<p>我的第一個段落。</p>
<script>
document.write(Date());
</script>
</body>
</html>
[編輯運行](http://www.runoob.com/try/tryit.php?filename=tryjs_output_alert)
**注意**:
請使用 document.write() 僅僅向文檔輸出寫內(nèi)容馏谨。如果在文檔已完成加載后執(zhí)行 document.write别渔,整個 HTML 頁面將被覆蓋。
>**實例**
<!DOCTYPE html>
<html>
<body>
<h1>我的第一個 Web 頁面</h1>
<p>我的第一個段落惧互。</p>
<button onclick="myFunction()">點我</button>
<script>
function myFunction() {
document.write(Date());
}
</script>
</body>
</html>
[編輯運行](http://www.runoob.com/try/tryit.php?filename=tryjs_output_alert)
####***寫到控制臺***
___________________
如果您的瀏覽器支持調(diào)試哎媚,你可以使用 console.log() 方法在瀏覽器中顯示 JavaScript 值。
瀏覽器中使用 F12 來啟用調(diào)試模式喊儡, 在調(diào)試窗口中點擊 "Console" 菜單拨与。
>**實例**
<!DOCTYPE html>
<html>
<body>
<h1>我的第一個 Web 頁面</h1>
<script>
a = 5;
b = 6;
c = a + b;
console.log(c);
</script>
</body>
</html>
[編輯運行](http://www.runoob.com/try/tryit.php?filename=tryjs_output_alert)