作業時間
試試看搭配 if else 語法做一個成績驗證系統
如果使用者輸入 大於等於 60 ,按下按鈕後會顯示文字 及格
如果使用者輸入 小於 60 ,按下按鈕後會顯示文字 不及格
Canvas
Canvas 是 Html 的元素 <canvas>
我們可以用 Canvas 來繪製圖形,就像用畫筆在畫布上畫畫一樣。

在這個章節中,我們將介紹如何讓 JS 操作網頁的資料 比如說取得使用者讀取的資料、送出文件、修改文字之類的
試試看搭配 if else 語法做一個成績驗證系統
如果使用者輸入 大於等於 60 ,按下按鈕後會顯示文字 及格
如果使用者輸入 小於 60 ,按下按鈕後會顯示文字 不及格
Canvas 是 Html 的元素 <canvas>
我們可以用 Canvas 來繪製圖形,就像用畫筆在畫布上畫畫一樣。
1<!DOCTYPE html>
2<html>
3
4<head>
5 <title>我的網頁</title>
6</head>
7
8<body>
9 <h1>這是一個標題</h1>
10 <div>
11 <h2>這是一個小標題</h2>
12 <p>這是一段文字。</p>
13 </div>
14</body>
15
16</html>
1<!DOCTYPE html>
2<html>
3
4<head>
5 <title>我的網頁</title>
6</head>
7
8<body>
9 <h1>這是一個標題</h1>
10 <div>
11 <h2 id="smallTitle">這是一個小標題</h2>
12 <p>這是一段文字。</p>
13 </div>
14</body>
15
16</html>1<!DOCTYPE html>
2<html>
3
4<head>
5 <title>我的網頁</title>
6</head>
7
8<body>
9 <h1>這是一個標題</h1>
10 <div>
11 <h2 id="smallTitle">這是一個小標題</h2>
12 <p>這是一段文字。</p>
13 </div>
14 <script>
15 console.log('Hi!')
16 </script>
17</body>
18
19</html>
1<!DOCTYPE html>
2<html>
3
4<head>
5 <title>我的網頁</title>
6</head>
7
8<body>
9 <h1>這是一個標題</h1>
10 <div>
11 <h2 id="smallTitle">這是一個小標題</h2>
12 <p>這是一段文字。</p>
13 </div>
14 <script>
15 let smallTitleElement = document.getElementById("smallTitle");
16 </script>
17</body>
18
19</html>
1<!DOCTYPE html>
2<html>
3
4<head>
5 <title>我的網頁</title>
6</head>
7
8<body>
9 <h1>這是一個標題</h1>
10 <div>
11 <h2 id="smallTitle">這是一個小標題</h2>
12 <p>這是一段文字。</p>
13 </div>
14 <script>
15 let smallTitleElement = document.getElementById("smallTitle");
16 smallTitleElement.style.color = "red";
17 </script>
18</body>
19
20</html>
1<!DOCTYPE html>
2<html>
3
4<head>
5 <title>我的網頁</title>
6</head>
7
8<body>
9 <h1>這是一個標題</h1>
10 <div>
11 <h2 id="smallTitle">這是一個小標題</h2>
12 <p>這是一段文字。</p>
13 </div>
14 <script>
15 let smallTitleElement = document.getElementById("smallTitle");
16 smallTitleElement.style.color = "red";
17 smallTitleElement.innerText = "這是一個紅色小標題"
18 </script>
19</body>
20
21</html>
1<!DOCTYPE html>
2<html>
3
4<head>
5 <title>我的網頁</title>
6</head>
7
8<body>
9 <h1>這是一個標題</h1>
10 <div>
11 <h2 id="smallTitle">這是一個小標題</h2>
12 <p>這是一段文字。</p>
13 <button id="myButton">這是一個按鈕</button>
14 </div>
15 <script>
16 let smallTitleElement = document.getElementById("smallTitle");
17 smallTitleElement.style.color = "red";
18 smallTitleElement.innerText = "這是一個紅色小標題";
19 </script>
20</body>
21
22</html>
1<!DOCTYPE html>
2<html>
3
4<head>
5 <title>我的網頁</title>
6</head>
7
8<body>
9 <h1>這是一個標題</h1>
10 <div>
11 <h2 id="smallTitle">這是一個小標題</h2>
12 <p>這是一段文字。</p>
13 <button id="myButton">這是一個按鈕</button>
14 </div>
15 <script>
16 let smallTitleElement = document.getElementById("smallTitle");
17 smallTitleElement.style.color = "red";
18 smallTitleElement.innerText = "這是一個紅色小標題";
19
20 let button = document.getElementById('myButton');
21 </script>
22</body>
23
24</html>
1<!DOCTYPE html>
2<html>
3
4<head>
5 <title>我的網頁</title>
6</head>
7
8<body>
9 <h1>這是一個標題</h1>
10 <div>
11 <h2 id="smallTitle">這是一個小標題</h2>
12 <p>這是一段文字。</p>
13 <button id="myButton">這是一個按鈕</button>
14 </div>
15 <script>
16 let smallTitleElement = document.getElementById("smallTitle");
17 smallTitleElement.style.color = "red";
18 smallTitleElement.innerText = "這是一個紅色小標題";
19
20 let button = document.getElementById("myButton");
21 button.addEventListener("click", () => {
22 smallTitleElement.style.color = "blue";
23 smallTitleElement.innerText = "這是一個藍色小標題";
24 });
25 </script>
26</body>
27
28</html>
1<!DOCTYPE html>
2<html>
3
4<head>
5 <title>我的網頁</title>
6</head>
7
8<body>
9 <h1>這是一個標題</h1>
10 <div>
11 <h2 id="smallTitle">這是一個小標題</h2>
12 <p>這是一段文字。</p>
13 <button id="myButton">這是一個按鈕</button>
14 <input id="myInput" type="text" placeholder="這是一個輸入框" />
15 </div>
16 <script>
17 let smallTitleElement = document.getElementById("smallTitle");
18 smallTitleElement.style.color = "red";
19 smallTitleElement.innerText = "這是一個紅色小標題";
20
21 let button = document.getElementById("myButton");
22 button.addEventListener("click", () => {
23 smallTitleElement.style.color = "blue";
24 smallTitleElement.innerText = "這是一個藍色小標題";
25 });
26 </script>
27</body>
28
29</html>
1<!DOCTYPE html>
2<html>
3
4<head>
5 <title>我的網頁</title>
6</head>
7
8<body>
9 <h1>這是一個標題</h1>
10 <div>
11 <h2 id="smallTitle">這是一個小標題</h2>
12 <p>這是一段文字。</p>
13 <button id="myButton">這是一個按鈕</button>
14 <input id="myInput" type="text" placeholder="這是一個輸入框" />
15 </div>
16 <script>
17 let smallTitleElement = document.getElementById("smallTitle");
18 smallTitleElement.style.color = "red";
19 smallTitleElement.innerText = "這是一個紅色小標題";
20
21 let myInputElement = document.getElementById("myInput");
22
23 let button = document.getElementById("myButton");
24 button.addEventListener("click", () => {
25 smallTitleElement.style.color = "blue";
26 smallTitleElement.innerText = "這是一個藍色小標題";
27 });
28 </script>
29</body>
30
31</html>
1<!DOCTYPE html>
2<html>
3
4<head>
5 <title>我的網頁</title>
6</head>
7
8<body>
9 <h1>這是一個標題</h1>
10 <div>
11 <h2 id="smallTitle">這是一個小標題</h2>
12 <p>這是一段文字。</p>
13 <button id="myButton">這是一個按鈕</button>
14 <input id="myInput" type="text" placeholder="這是一個輸入框" />
15 </div>
16 <script>
17 let smallTitleElement = document.getElementById("smallTitle");
18 smallTitleElement.style.color = "red";
19 smallTitleElement.innerText = "這是一個紅色小標題";
20
21 let myInputElement = document.getElementById("myInput");
22
23 let button = document.getElementById("myButton");
24 button.addEventListener("click", () => {
25 let inputValue = myInputElement.value;
26 smallTitleElement.innerText = inputValue;
27 });
28 </script>
29</body>
30
31</html>
1<!DOCTYPE html>
2<html>
3
4<head>
5 <title>我的網頁</title>
6</head>
7
8<body>
9 <h1>這是一個標題</h1>
10 <div>
11 <h2 id="smallTitle">這是一個小標題</h2>
12 <p>這是一段文字。</p>
13 <button id="myButton">這是一個按鈕</button>
14 <input id="myInput" type="text" placeholder="這是一個輸入框" />
15 </div>
16 <script>
17 let smallTitleElement = document.getElementById("smallTitle");
18 smallTitleElement.style.color = "red";
19 smallTitleElement.innerText = "這是一個紅色小標題";
20
21 let myInputElement = document.getElementById("myInput");
22
23 let button = document.getElementById("myButton");
24 button.addEventListener("click", () => {
25 let inputValue = myInputElement.value;
26 smallTitleElement.innerText = inputValue;
27 myInputElement.value = "";
28 });
29 </script>
30</body>
31
32</html>
1<!DOCTYPE html>
2<html>
3
4<head>
5 <title>我的網頁</title>
6</head>
7
8<body>
9 <input id="scoreInput" type="text" placeholder="輸入成績" />
10 <button id="checkButton">檢查分數</button>
11 <div id="myScoreState"></div>
12
13 <script>
14 let scoreInput = document.getElementById("scoreInput");
15 let checkButton = document.getElementById("checkButton");
16 let myScoreState = document.getElementById("myScoreState");
17
18 checkButton.addEventListener("click", function () {
19 let score = parseInt(scoreInput.value);
20 if (score >= 60) {
21 myScoreState.textContent = "恭喜你及格了!";
22 } else {
23 myScoreState.textContent = "抱歉你不及格!";
24 }
25 });
26 </script>
27</body>
28
29</html>1<!DOCTYPE html>
2<html>
3
4<head>
5 <title>Canvas 動畫</title>
6</head>
7
8<body>
9 <canvas id="myCanvas" width="400" height="400"></canvas>
10</body>
11
12</html>
1<!DOCTYPE html>
2<html>
3 <head>
4 <title>Canvas 動畫</title>
5 </head>
6 <body>
7 <canvas id="myCanvas" width="400" height="400"></canvas>
8 <script>
9 const canvas = document.getElementById("myCanvas");
10 </script>
11 </body>
12</html>
13
1<!DOCTYPE html>
2<html>
3
4<head>
5 <title>Canvas 動畫</title>
6</head>
7
8<body>
9 <canvas id="myCanvas" width="400" height="400"></canvas>
10 <script>
11 const canvas = document.getElementById("myCanvas");
12 const ctx = canvas.getContext("2d");
13 </script>
14</body>
15
16</html>
1<!DOCTYPE html>
2<html>
3
4<head>
5 <title>Canvas 動畫</title>
6</head>
7
8<body>
9 <canvas id="myCanvas" width="400" height="400"></canvas>
10 <script>
11 const canvas = document.getElementById("myCanvas");
12 const ctx = canvas.getContext("2d");
13
14 ctx.fillStyle = "blue";
15 </script>
16</body>
17
18</html>
1<!DOCTYPE html>
2<html>
3
4<head>
5 <title>Canvas 動畫</title>
6</head>
7
8<body>
9 <canvas id="myCanvas" width="400" height="400"></canvas>
10 <script>
11 const canvas = document.getElementById("myCanvas");
12 const ctx = canvas.getContext("2d");
13
14 ctx.fillStyle = "blue";
15 ctx.fillRect(50, 50, 150, 150);
16 </script>
17</body>
18
19</html>