HTML 中的 input 標簽詳解
一、基礎概念
1. 定義與作用
HTML 中的 <input>
標簽是表單元素的核心組件,用于創建各種用戶輸入字段。作為一個空標簽(沒有閉合標簽),它通過 type
屬性來決定呈現何種輸入控件,是實現用戶與網頁交互的基礎元素。
2. 基本語法
<input type="text" name="username" value="默認值">
type
:指定輸入類型,如text
、password
、radio
等。name
:表單提交時的鍵名,用于服務器端接收數據。value
:輸入的值,不同類型有不同表現。
二、常見 input 類型
1. 文本輸入(text)
最基本的輸入類型,用于單行文本輸入。
<input type="text" name="username" placeholder="請輸入用戶名">
placeholder
:提供提示文本,輸入內容時自動消失。
2. 密碼輸入(password)
輸入內容會被掩碼(通常用圓點)隱藏。
<input type="password" name="password" placeholder="請輸入密碼">
3. 單選按鈕(radio)
用于從多個選項中選擇一個。
<input type="radio" name="gender" value="male" checked> 男
<input type="radio" name="gender" value="female"> 女
- 同一組單選按鈕需使用相同的
name
屬性。 checked
屬性設置默認選中項。
4. 復選框(checkbox)
用于多選場景。
<input type="checkbox" name="hobby" value="reading" checked> 閱讀
<input type="checkbox" name="hobby" value="swimming"> 游泳
checked
屬性設置默認選中項。
5. 文件上傳(file)
允許用戶上傳文件。
<input type="file" name="avatar" accept="image/*">
accept
:限制上傳文件類型,如image/*
表示所有圖片格式。
6. 提交按鈕(submit)
用于提交表單數據到服務器。
<input type="submit" value="提交">
7. 重置按鈕(reset)
用于重置表單所有輸入項為初始值。
<input type="reset" value="重置">
8. 隱藏字段(hidden)
用于存儲不顯示給用戶但隨表單提交的數據。
<input type="hidden" name="userId" value="123">
三、HTML5 新增 input 類型
1. 電子郵件(email)
專門用于輸入電子郵件地址,支持瀏覽器內置驗證。
<input type="email" name="email" placeholder="your@email.com">
2. 網址(url)
用于輸入 URL,自動驗證格式。
<input type="url" name="website" placeholder="https://example.com">
3. 數字(number)
只能輸入數字,支持 min
、max
和 step
屬性。
<input type="number" name="age" min="1" max="100" step="1">
4. 范圍選擇(range)
通過滑塊選擇值,需指定 min
、max
和 step
。
<input type="range" name="volume" min="0" max="100" step="5" value="50">
5. 日期與時間
date
:選擇日期(年-月-日)time
:選擇時間(時:分:秒)datetime-local
:選擇日期和時間
<input type="date" name="birthdate">
<input type="time" name="appointment">
<input type="datetime-local" name="meeting">
6. 搜索框(search)
外觀與文本框相似,但在某些瀏覽器中會有特殊處理(如清除按鈕)。
<input type="search" name="query" placeholder="搜索...">
7. 電話號碼(tel)
用于輸入電話號碼,但不強制驗證格式(因全球格式差異大)。
<input type="tel" name="phone" placeholder="請輸入電話號碼">
四、重要屬性詳解
1. 表單相關屬性
name
:表單提交時的鍵名。value
:輸入的值,對提交起作用。disabled
:禁用輸入框,值不會被提交。readonly
:只讀模式,值會被提交但不可編輯。
2. 驗證屬性
required
:必填項,提交前必須填寫。minlength
和maxlength
:限制文本長度。min
和max
:限制數字或日期范圍。pattern
:使用正則表達式驗證輸入格式。
<input type="text" name="zipcode" pattern="[0-9]{6}" placeholder="郵政編碼">
3. 輔助屬性
placeholder
:提示文本。autofocus
:頁面加載時自動聚焦。autocomplete
:啟用或禁用自動完成功能(on
或off
)。spellcheck
:啟用或禁用拼寫檢查(true
或false
)。
五、事件處理
<input>
標簽支持多種事件,可通過 JavaScript 監聽并響應。
1. 常見事件
onchange
:值改變且失去焦點時觸發。oninput
:值改變時實時觸發。onfocus
:獲得焦點時觸發。onblur
:失去焦點時觸發。
2. 示例代碼
<input type="text" id="username" oninput="validate(this)">
<script>function validate(input) {if (input.value.length < 3) {input.style.borderColor = 'red';} else {input.style.borderColor = 'green';}}
</script>
六、樣式與美化
雖然 <input>
的外觀受瀏覽器默認樣式影響,但可通過 CSS 進行定制。
1. 基本樣式修改
input[type="text"] {width: 100%;padding: 10px;border: 1px solid #ccc;border-radius: 4px;box-sizing: border-box;
}
2. 自定義單選按鈕和復選框
通過隱藏原生控件,使用 CSS 創建自定義樣式。
<style>.custom-checkbox input {display: none;}.custom-checkbox span {display: inline-block;width: 20px;height: 20px;border: 1px solid #ccc;border-radius: 3px;}.custom-checkbox input:checked + span {background-color: #2196F3;}
</style>
<div class="custom-checkbox"><input type="checkbox" id="customCheck"><span></span><label for="customCheck">自定義復選框</label>
</div>
七、與 JavaScript 的交互
通過 JavaScript 可動態操作 <input>
元素。
1. 獲取和設置值
const input = document.getElementById('username');
// 獲取值
const value = input.value;
// 設置值
input.value = '新值';
2. 動態禁用/啟用
input.disabled = true; // 禁用
input.disabled = false; // 啟用
3. 表單驗證
function validateForm() {const email = document.getElementById('email');if (!email.checkValidity()) {alert('請輸入有效的電子郵件地址');return false;}return true;
}
八、無障礙性考慮
為提高可訪問性,應注意以下幾點:
1. 使用 label 標簽
<label for="username">用戶名:</label>
<input type="text" id="username" name="username">
2. 添加 ARIA 屬性
<input type="text" id="search" aria-label="搜索產品">
3. 提供錯誤提示
<input type="email" id="email" aria-describedby="emailError">
<span id="emailError" class="error" aria-live="polite"></span>
九、實際應用場景
1. 用戶注冊表單
包含文本、密碼、單選、復選框等多種輸入類型。
2. 搜索功能
使用 search
類型或普通文本框。
3. 數據篩選
結合 range
和 number
實現數值范圍篩選。
4. 文件上傳
圖片、文檔等資源上傳功能。
十、兼容性與注意事項
- 新的 input 類型在舊瀏覽器(如 IE)中可能降級為文本框。
- 不同瀏覽器對樣式和驗證的實現可能略有差異。
- 使用自定義樣式時需注意保持交互體驗一致。
通過合理使用 <input>
標簽及其屬性,可以創建出功能豐富、用戶友好的表單界面,滿足各種 Web 應用的需求。