以下為你整理 HTML5 最值得掌握的新特性與實用技巧:
1. 全新的 Doctype
只需使用 <!DOCTYPE html>,即使瀏覽器不認識這句話,也會以標準模式渲染頁面。
2. Figure 元素
用 <figure> 與 <figcaption> 語義化地表示帶標題的圖片:
<figure>
<img src="path/to/image" alt="About image" />
<figcaption>
<p>This is an image of something interesting.</p>
</figcaption>
</figure>
3. 重新定義的 <small>
<small> 已被重新定義,現在用來表示小型排版內容,例如網站底部的版權聲明。
4. 移除 link 與 script 標籤的 type 屬性
不再需要額外指定 type,瀏覽器能自行判斷。
5. 加或不加引號
HTML5 對屬性是否加引號、標籤是否閉合並無嚴格要求,但仍建議加上引號並閉合標籤,以保持代碼整潔。
6. 讓內容可編輯
只要加上 contenteditable 屬性,就能讓內容變成可編輯狀態。
7. Email 輸入框
將 input 的 type 設為 email,瀏覽器便會驗證輸入是否為 Email 格式。當然,前端校驗之外,後端仍需相應的驗證。
8. Placeholder 佔位符
不必再靠 JavaScript 就能實現佔位提示文字的效果。
9. Local Storage
使用 Local Storage 能在客戶端永久儲存大型資料片段(除非主動刪除)。目前大多數瀏覽器已支援,使用前可先偵測 window.localStorage 是否存在。
10. 語義化的 header 與 footer
用 header、footer 標籤取代泛用的 div,讓頁面結構更清晰。
11. 更多 HTML5 表單特性
善用日期、數字、range 等新表單類型,提升輸入體驗。
12. IE 與 HTML5
HTML5 新元素預設以 inline 方式渲染,可用以下方式讓它們以 block 顯示:
header, footer, article, section, nav, menu, hgroup {
display: block;
}
不幸的是 IE 會忽略這些樣式,可這樣修復:
document.createElement("article");
document.createElement("footer");
document.createElement("header");
document.createElement("hgroup");
document.createElement("nav");
document.createElement("menu");
13. hgroup
一般在 header 中將一組標題組合在一起:
<header>
<hgroup>
<h1>Recall Fan Page</h1>
<h2>Only for people who want the memory of a lifetime.</h2>
</hgroup>
</header>
14. Required 屬性
required 定義某個 input 是否為必填:
<input type="text" name="someInput" required>
或寫成 required="required"。
15. Autofocus 屬性
顧名思義,讓輸入框自動獲得焦點:
<input type="text" name="someInput" placeholder="Douglas Quaid" required autofocus>
16. Audio 支援
HTML5 提供 <audio> 標籤,不必再依賴第三方外掛即可播放音訊。大多數現代瀏覽器已支援,但目前仍需一些相容處理:
<audio autoplay="autoplay" controls="controls">
<source src="file.ogg" /><!--FF-->
<source src="file.mp3" /><!--Webkit-->
<a href="file.mp3">Download this file.</a>
</audio>
17. Video 支援
與 Audio 類似,<video> 標籤提供影片支援。由於 HTML5 未指定單一編碼,瀏覽器各自決定支援哪些格式,導致不一致:Safari 與 IE 支援 H.264,Firefox 與 Opera 支援 Theora 與 Vorbis。因此使用 HTML5 影片時,最好同時提供多種格式:
<video controls preload>
<source src="cohagenPhoneCall.ogv" type='video/ogg; codecs="vorbis, theora"' />
<source src="cohagenPhoneCall.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"' />
<p>Your browser is old. <a href="cohagenPhoneCall.mp4">Download this video instead.</a></p>
</video>
18. 預加載影片
preload 屬性如其字面意,讓你決定是否在頁面載入時預先加載影片:<video preload>
19. 顯示影片控制列
<video preload controls>
20. 正規表達式
透過 pattern 屬性,可直接在標記中使用正規表達式:
<form action="" method="post">
<label for="username">Create a Username: </label>
<input type="text" name="username" id="username" placeholder="4 <> 10" pattern="[A-Za-z]{4,10}" autofocus required>
<button type="submit">Go</button>
</form>
21. 偵測屬性支援
除了 Modernizr,也能用簡單的 JavaScript 偵測屬性是否支援:
<script>
if (!'pattern' in document.createElement('input')) {
// do client/server side validation
}
</script>
22. Mark 元素
把 <mark> 視為高亮標記。當使用者搜尋一段文字時,其效果應如下:
<h3>Search Results</h3>
<p>They were interrupted, just after Quato said, <mark>"Open your Mind"</mark>.</p>
23. 何時該用 <div>
HTML5 引入了這麼多語義元素,那 div 還要用嗎?答案是:當沒有更合適的元素時,就用 div。
24. 想立刻使用 HTML5?
別再等 2022 年了,現在就能用,Just do it。
25. 哪些不算 HTML5
SVG、CSS3、Geolocation、Client Storage、Web Sockets 嚴格來說都不是 HTML5 的一部分。
26. Data 屬性
<div id="myDiv" data-custom-attr="My Value">Bla Bla</div>
可在 CSS 中取用:
h1:hover:after {
content: attr(data-hover-response);
color: black;
position: absolute;
left: 0;
}
27. Output 元素
<output> 用來顯示計算結果,同樣具備與 label 類似的 for 屬性。
28. 用 Range Input 製作滑桿
HTML5 的 range 類型可建立滑桿,接受 min、max、step 與 value 屬性,還能用 CSS 的 :before 與 :after 顯示最小與最大值:
<input type="range" name="range" min="0" max="10" step="1" value="">
input[type=range]:before { content: attr(min); padding-right: 5px; }
input[type=range]:after { content: attr(max); padding-left: 5px; }