DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> HTML基礎知識 >> 關於HTML
HTML5 值得掌握的 28 個新特性
編輯:關於HTML     

HTML5 帶來的改變遠不只是多幾個標籤。以下按主題整理 28 個實用特性,並附上可直接參考的語法。

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>

現在用來表示小型附註文字,例如頁面底部的版權聲明。

4. link 與 script 不再需要 type

type 屬性可以省略,瀏覽器預設就當成 CSS 與 JavaScript 處理。

5. 引號與標籤閉合不再強制

HTML5 不強制屬性加引號、也不強制閉合標籤,但仍強烈建議照 XHTML 的習慣寫,可讀性與工具相容性都更好。

6. contenteditable

加上這個屬性,元素內容就變成可直接編輯,過去得靠 input 或 textarea 才能做到。

7. Email 輸入型別

<input type="email">

瀏覽器會驗證輸入是否為 email 格式。但前端驗證只能當提示,後端仍必須再做一次檢查。

8. Placeholder

不必再靠 JavaScript 製作提示文字,直接用 placeholder 屬性即可。

9. Local Storage

可在客戶端長期保存大量資料(除非主動清除)。使用前一天建議先偵測 window.localStorage 是否存在,並記得以 try/catch 包住存取,避免無痕模式拋出例外。

10. 語意化的 header 與 footer

取代一堆匿名 div,讓文件大綱可被機器理解。

11. 更豐富的表單控制項

date、time、number、range、color 等型別大幅減少了自製元件的負擔。

12. IE 與 HTML5 的相容處理

新元素預設以 inline 渲染,可用 CSS 改為 block:

header, footer, article, section, nav, menu, hgroup {
  display: block;
}

但舊版 IE 會忽略這些樣式,需靠 JS 補救:

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 屬性

<input type="text" name="someInput" required>
<!-- 或者 -->
<input type="text" name="someInput" required="required">

15. autofocus 屬性

<input type="text" name="someInput" placeholder="Douglas Quaid" required autofocus>

16. Audio 支援

<audio autoplay="autoplay" controls="controls">
  <source src="file.ogg" />            <!-- Firefox -->
  <source src="file.mp3" />            <!-- WebKit -->
  <a href="file.mp3">Download this file.</a>
</audio>

不必再安裝第三方外掛即可播放音訊,但因為各家瀏覽器支援的編碼不同,仍需提供多種格式作為後備。

17. Video 支援

HTML5 並未指定影片編碼,交由瀏覽器自行決定,因此造成不少分歧:Safari 與 IE 支援 H.264,Firefox 與 Opera 支援 Theora/Vorbis。實務上必須同時提供多種來源:

<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,也能用簡單的 JS 判斷:

<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 還要用嗎?

要。div 的定位是「沒有更合適的語意標籤時才使用」的通用容器,而不是不能用。

24. 現在就能用 HTML5

不必等標準完全定案,今日的瀏覽器早已普遍支援。

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;
}
<h1 data-hover-response="I Said Don't Touch Me!">Don't Touch Me</h1>

27. Output 元素

用來顯示計算結果,同樣具備與 label 一致的 for 屬性,能明確表達它與哪些輸入欄位相關。

28. 用 Range 做滑桿

<input type="range" name="range" min="0" max="10" step="1" value="">

接受 min、max、step、value 四個屬性,還能搭配 CSS 的 ::before 與 ::after 把最大最小值顯示出來。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved