<input type="text" name="username" id="nameId" />
<span id="uspan"></span>
<label for="nameId"> 關聯標籤、JavaScript 取值(document.getElementById('nameId').value)、CSS 樣式定位、表單驗證等。document.getElementById('uspan').innerText = '訊息' 更新內容。<input type="text" id="email" onblur="checkEmail()" />
<span id="emailTip" style="color:red"></span>
<script>
function checkEmail() {
const val = document.getElementById('email').value;
const tip = document.getElementById('emailTip');
tip.textContent = val.includes('@') ? '' : '郵件格式錯誤';
}
</script>
元素類型_功能,如 input_username、span_usernameTip。getElementById 僅回傳第一個元素,導致邏輯錯誤。class 或 data-* 屬性配合 querySelectorAll。理解各元素 id 的獨立職責,能編寫結構清晰、易維護的前端程式碼。