`` 是 HTML5 專為搜尋場景設計的輸入類型,提供**語意化標記**、**平台原生搜尋 UI** (如清除按鈕、圓角、內建放大鏡圖示)、**自動完成/歷史記錄** 整合,以及 **CSS 偽元素** 精細樣式控制。
| 屬性 | 說明 | 範例值 |
|---|---|---|
placeholder | 佔位提示文字 | "搜尋商品、品牌..." |
autosave | **WebKit/Blink 專屬**:指定自動儲存歷史記錄的唯一識別字串,同域名下相同 autosave 值共享歷史 | "mysite-search-history" |
results | **WebKit/Blink 專屬**:下拉歷史記錄顯示筆數上限 | "5" 或 5 |
incremental | **WebKit/Blink 專屬**:布林屬性,輸入時即時發送 `onsearch` 事件 (即時搜尋建議) | 存在即真 |
list | 關聯 ` | "search-suggestions" |
name | 表單提交鍵名 | "q" |
value | 初始值 | "" |
<!DOCTYPE html>
<html lang="zh-Hant">
<head>
<meta charset="UTF-8">
<title>HTML5 Search Input Demo</title>
<style>
/* 重置 WebKit/Blink 預設樣式 */
input[type="search"] {
-webkit-appearance: none; /* 移除原生圓角、內邊距、放大鏡 */
appearance: none;
box-sizing: border-box;
padding: 0.5rem 2.5rem 0.5rem 1rem; /* 右側留空給清除按鈕 */
border: 1px solid #ccc;
border-radius: 9999px; /* 胶囊圆角 */
font-size: 1rem;
width: 100%;
max-width: 400px;
}
/* 自定義清除按鈕 (WebKit/Blink) */
input[type="search"]::-webkit-search-cancel-button {
-webkit-appearance: none;
appearance: none;
width: 1.25rem;
height: 1.25rem;
background: url("data:image/svg+xml,%3Csvg xmlns='https://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%23666'%3E%3Cpath d='M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z'/%3E%3C/svg%3E") center/contain no-repeat;
cursor: pointer;
}
input[type="search"]::-webkit-search-cancel-button:hover {
background-color: rgba(0,0,0,0.1);
border-radius: 50%;
}
/* Firefox 清除按鈕 (需啟用 layout.css.search-cancel-button.enabled) */
input[type="search"]::-moz-search-clear { display: none; } /* Firefox 預設無,可自行實作 */
</style>
</head>
<body>
<form action="/search" method="GET" role="search">
<label for="site-search" class="visually-hidden">站內搜尋</label>
<input type="search"
id="site-search"
name="q"
placeholder="搜尋文章、教程..."
autosave="mysite-search"
results="5"
incremental
list="search-suggestions">
<datalist id="search-suggestions">
<option value="HTML5 教學">
<option value="CSS Grid 佈局">
<option value="JavaScript 非同步">
<option value="ASP.NET Core 部署">
<option value="Docker 入門">
</datalist>
<button type="submit">搜尋</button>
</form>
<script>
const searchInput = document.getElementById('site-search');
// 監聽 incremental 即時搜尋事件 (WebKit/Blink)
searchInput.addEventListener('search', e => {
console.log('即時搜尋:', e.target.value);
// 這裡可發送 AJAX 獲取搜尋建議,更新 datalist
});
// 表單提交前驗證
searchInput.form.addEventListener('submit', e => {
if (!searchInput.value.trim()) {
e.preventDefault();
alert('請輸入搜尋關鍵字');
}
});
</script>
</body>
</html>
| 瀏覽器引擎 | 預設外觀 | 關鍵偽元素 | 建議策略 |
|---|---|---|---|
| WebKit/Blink (Chrome, Edge, Safari, Opera) | 圓角、內建放大鏡、清除按鈕 (有值時顯示) | `::-webkit-search-cancel-button`, `::-webkit-search-decoration`, `::-webkit-search-results-button`, `::-webkit-search-results-decoration` | `appearance: none` 完全重置,自行實作圖示與清除按鈕 |
| Gecko (Firefox) | 方框、無清除按鈕 (預設) | 無標準偽元素 (實驗性 `::-moz-search-clear` 需啟用旗標) | 同 `appearance: none`,JS 監聽 `input` 事件動態顯示/隱藏自製清除按鈕 |
const searchInput = document.getElementById('site-search');
const datalist = document.getElementById('search-suggestions');
let debounceTimer;
searchInput.addEventListener('input', () => {
clearTimeout(debounceTimer);
debounceTimer = setTimeout(async () => {
const q = searchInput.value.trim();
if (q.length < 2) { datalist.innerHTML = ''; return; }
try {
const resp = await fetch(`/api/search/suggest?q=${encodeURIComponent(q)}`);
const suggestions = await resp.json(); // ["關鍵字1", "關鍵字2", ...]
datalist.innerHTML = suggestions.map(s => `
`` 提供語意化與平台原生增強體驗,但**跨瀏覽器 UI 差異極大**,核心屬性 (`autosave`, `results`, `incremental`) 多為 WebKit/Blink 專屬。生產環境建議: