使用 innerHTM
<!DOCTYPE html>
<html>
<head>
<title>修改 HTML 內容</title>
</head>
<body>
<div id="info">原始內容</div>
<button onclick="updateContent()">更新內容</button>
<script>
function updateContent() {
// 取得 id 為 info 的元素
var element = document.getElementById("info");
// 修改該元素的 HTML 內容
element.innerHTML = "<strong>內容已更新!</strong>";
}
</script>
</body>
</html>
使用 innerText
<!DOCTYPE html>
<html>
<head>
<title>修改文字內容</title>
</head>
<body>
<p id="greeting">早安!</p>
<button onclick="changeText()">改成晚安</button>
<script>
function changeText() {
// 取得 id 為 greeting 的元素
var element = document.getElementById("greeting");
// 修改該元素的純文字內容
element.innerText = "晚安!";
}
</script>
</body>
</html>
錄製內容 2025-08-29 144950
使用 document.write()
// 在測試階段使用 document.write(),可快速輸出內容到網頁
document.write("<h1>測試成功!</h1>");
<!DOCTYPE html>
<html>
<body>
<h2>Hello</h2>
<p>Hello</p>
<button type="button" onclick="document.write('測試成功')">清除整個 DOM 並重新寫入內容</button>
</body>
</html>
Adobe Express - 錄製內容 2025-08-29 144950 (1)
使用 window.alert()
<!DOCTYPE html>
<html>
<body>
<button onclick="showAlert()">點我顯示警告</button>
<script>
function showAlert() {
window.alert("這是一個警告訊息!");
}
</script>
</body>
</html>
錄製內容 2025-08-29 144950 (2)
使用 console.log()
<!DOCTYPE html>
<html>
<head>
<title>Console.log 範例</title>
</head>
<body>
<script>
let name = "小明";
let age = 25;
console.log("使用者名稱:", name);
console.log(`年齡是:${age}`);
</script>
</body>
</html>
image
JavaScript 列印
<!DOCTYPE html>
<html>
<body>
<h2>window.print()方法</h2>
<p>列應這個網頁</p>
<button onclick="window.print()">列應</button>
</body>
</html>