inputmode
?是一個 HTML 屬性,用于指定用戶在編輯元素或其內容時應使用的虛擬鍵盤布局類型。它主要影響移動設備和平板電腦的輸入體驗。
語法
<input inputmode="value">
<!-- 或 -->
<textarea inputmode="value"></textarea>
<!-- 或 -->
<div contenteditable="true" inputmode="value"></div>
屬性值
值 | 描述 | 適用場景 |
---|---|---|
none | 不顯示虛擬鍵盤 | 當輸入由自定義鍵盤控制時 |
text | 默認文本鍵盤 | 通用文本輸入 |
decimal | 數字鍵盤,可能包含小數點和負號 | 數字輸入,特別是小數 |
numeric | 純數字鍵盤 | 電話號碼、驗證碼等 |
tel | 電話號碼鍵盤(通常包含*和#) | 電話號碼輸入 |
search | 優化的搜索鍵盤(可能有搜索按鈕) | 搜索框 |
email | 優化的電子郵件鍵盤(通常包含@和.) | 電子郵件地址輸入 |
url | 優化的URL鍵盤(通常包含/和.com按鈕) | 網址輸入 |
?示例
<!-- 數字輸入 -->
<input type="text" inputmode="numeric" pattern="[0-9]*" placeholder="請輸入驗證碼"><!-- 電子郵件輸入 -->
<input type="text" inputmode="email" placeholder="請輸入郵箱"><!-- 電話號碼輸入 -->
<input type="tel" inputmode="tel" placeholder="請輸入電話號碼"><!-- 搜索框 -->
<input type="search" inputmode="search" placeholder="搜索...">
注意事項
-
inputmode
?不是驗證工具,仍需使用?pattern
?或 JavaScript 進行輸入驗證 -
不同瀏覽器和設備對?
inputmode
?的實現可能略有差異 -
對于?
contenteditable
?元素,某些瀏覽器可能不完全支持 -
可以與其他輸入屬性(如?
pattern
、maxlength
)結合使用
瀏覽器支持
大多數現代瀏覽器(包括移動瀏覽器)都支持?inputmode
?屬性,但在使用時最好進行兼容性測試。