新千年以來,超文本標記語言(HTML)5 第一次向 HTML 中引入新的元素。新的結構元素包括aside
、figure
和section
。新的內聯元素包括time
、meter
和progress
。新的內嵌元素有video
和audio
。新的交互元素有details
、datagrid
和command
。
超文本標記語言(HTML)的開發到 1999 年 HTML 4 就停止了。萬維網聯盟(W3C)把重點轉向將 HTML 的底層語法從標準通用標記語言(SGML)改爲可擴展標記語言(XML),以及可縮放向量圖型(SVG)、XForms 和 MathML 這些全新的標記語言。瀏覽器廠商則把精力放到選項卡和富站點摘要(RSS)閱讀器這類瀏覽器特性上。Web 設計人員開始學習使用異步 JavaScript + XML(Ajax),在現有的框架下通過層疊樣式表(CSS)和 JavaScript™ 語言建立自己的應用程序。但是在接下來的八年中,HTML 本身沒有任何變化。
最近,它又復活了。三家重要的瀏覽器廠商 — Apple、Opera 和 Mozilla Foundation — 成立了 Web Hypertext Application Technology Working Group(WhatWG)來開發傳統 HTML 的新版本。最近,W3C 也注意到了這些活動,也啓動了自己的新一代 HTML 項目,雙方的成員有很多是相同的。這兩個項目最終很可能合併。雖然很多細節還在爭論之中,但下一版本 HTML 的大體輪廓已經清楚了。
Web 開發人員從 1999 年就一直期待 HTML 的新版本(通常稱爲 HTML 5,但是也稱爲 Web Applications 1.0),現在它終於發佈了。它保持了 HTML 原來的特色:沒有名稱空間或模式。元素不必結束。瀏覽器會寬容地對待錯誤。p
仍然是 p
,table
仍然是 table
。
如果在 1999 年將一位 Web 開發人員冷凍起來,現在再解凍,那麼他會遇到一些新的讓人迷惑的元素。是的,他熟悉的元素(比如 div
)仍然保留了;但是,HTML 現在還包含 section
、header
、footer
和 nav
等新元素。em
、code
和 strong
仍然存在,但是增加了 meter
、time
和 m
。img
和 embed
仍然可用,但是還增加了 video
和 audio
。但是,他仔細觀察一下就會發現,這些元素實際上沒什麼區別。其中許多元素可能在 1999 年是開發人員需要而沒有得到的。通過與他已經掌握的元素進行簡單的類比,這些新元素都很容易理解。實際上,與 Ajax 或 CSS 相比,它們非常容易掌握。
最後,當他打開 300MHz 的筆記本(運行的是 Windows 98,也是在 1999 年冷凍起來的)時,他可能對 Netscape 4 和 Windows® Internet Explorer® 5 中顯示的新頁面效果很吃驚。當然,這些老式瀏覽器不認識新元素,會完全忽略它們,但是頁面仍然會顯示,內容仍然是完整的。
這並不是什麼虛構的故事。HTML 5 的設計原則就是在不支持它的瀏覽器中能夠平穩地退化。原因很簡單:我們都是這樣的 “原始人”。瀏覽器現在有選項卡、CSS 和 XmlHttpRequest,但是它們的 HTML 顯示引擎仍然停留在 1999 年的水平。除了用戶量大大增加之外,Web 其實在本質上沒什麼進步。HTML 5 考慮到了這一點。它目前爲 Web 開發人員一些真正的好處,隨着瀏覽器的緩慢升級,頁面瀏覽者會逐漸享受到這些好處。我們來看看 HTML 5 提供了什麼。
由於缺少結構,即使是形式良好的 HTML 頁面也比較難以處理。必須分析標題的級別,才能看出各個部分的劃分方式。邊欄、頁腳、頁眉、導航條、主內容區和各篇文章都由通用的 div
元素來表示。HTML 5 添加了一些新元素,專門用來標識這些常見的結構:
section
:這可以是書中的一章或一節,實際上可以是在 HTML 4 中有自己的標題的任何東西header
:頁面上顯示的頁眉;與head
元素不一樣footer
:頁腳;可以顯示電子郵件中的簽名nav
:指向其他頁面的一組鏈接article
:blog、雜誌、文章彙編等中的一篇文章
我們來考慮一個典型的 blog 主頁,它的頂部有頁眉,底部有頁腳,還有幾篇文章、一個導航區和一個邊欄,見清單 1。
清單 1. 典型的 blog 頁面
<html> <head> <title>Mokka mit Schlag </title> </head> <body> <div id="page"> <div id="header"> <h1><a href="http://www.elharo.com/blog">Mokka mit Schlag</a></h1> </div> <div id="container"> <div id="center" class="column"> <div class="post" id="post-1000572"> <h2><a href= "/blog/birding/2007/04/23/spring-comes-and-goes-in-sussex-county/"> Spring Comes (and Goes) in Sussex County</a></h2> <div class="entry"> <p>Yesterday I joined the Brooklyn Bird Club for our annual trip to Western New Jersey, specifically Hyper Humus, a relatively recently discovered hot spot. It started out as a nice winter morning when we arrived at the site at 7:30 A.M., progressed to Spring around 10:00 A.M., and reached early summer by 10:15. </p> </div> </div> <div class="post" id="post-1000571"> <h2><a href= "/blog/birding/2007/04/23/but-does-it-count-for-your-life-list/"> But does it count for your life list?</a></h2> <div class="entry"> <p>Seems you can now go <a href="http://www.wired.com/science/discoveries/news/ 2007/04/cone_sf">bird watching via the Internet</a>. I haven't been able to test it out yet (20 user limit apparently) but this is certainly cool. Personally, I can't imagine it replacing actually being out in the field by any small amount. On the other hand, I've always found it quite sad to meet senior birders who are no longer able to hold binoculars steady or get to the park. I can imagine this might be of some interest to them. At least one elderly birder did a big year on TV, after he could no longer get out so much. This certainly tops that.</p> </div> </div> </div> <div class="navigation"> <div class="alignleft"> <a href="/blog/page/2/">« Previous Entries</a> </div> <div class="alignright"></div> </div> </div> <div id="right" class="column"> <ul id="sidebar"> <li><h2>Info</h2> <ul> <li><a href="/blog/comment-policy/">Comment Policy</a></li> <li><a href="/blog/todo-list/">Todo List</a></li> </ul></li> <li><h2>Archives</h2> <ul> <li><a href='/blog/2007/04/'>April 2007</a></li> <li><a href='/blog/2007/03/'>March 2007</a></li> <li><a href='/blog/2007/02/'>February 2007</a></li> <li><a href='/blog/2007/01/'>January 2007</a></li> </ul> </li> </ul> </div> <div id="footer"> <p>Copyright 2007 Elliotte Rusty Harold</p> </div> </div> </body> </html> |
即使有正確的縮進,這些嵌套的 div
仍然讓人覺得非常混亂。在 HTML 5 中,可以將這些元素替換爲語義性的元素,見清單 2。
清單 2. 用 HTML 5 編寫的典型 blog 頁面
<html> <head> <title>Mokka mit Schlag </title> </head> <body> <header> <h1><a href="http://www.elharo.com/blog">Mokka mit Schlag</a></h1> </header> <section> <article> <h2><a href= "/blog/birding/2007/04/23/spring-comes-and-goes-in-sussex-county/"> Spring Comes (and Goes) in Sussex County</a></h2> <p>Yesterday I joined the Brooklyn Bird Club for our annual trip to Western New Jersey, specifically Hyper Humus, a relatively recently discovered hot spot. It started out as a nice winter morning when we arrived at the site at 7:30 A.M., progressed to Spring around 10:00 A.M., and reached early summer by 10:15. </p> </article> <article> <h2><a href= "/blog/birding/2007/04/23/but-does-it-count-for-your-life-list/"> But does it count for your life list?</a></h2> <p>Seems you can now go <a href="http://www.wired.com/science/discoveries/news/ 2007/04/cone_sf">bird watching via the Internet</a>. I haven't been able to test it out yet (20 user limit apparently) but this is certainly cool. Personally, I can't imagine it replacing actually being out in the field by any small amount. On the other hand, I've always found it quite sad to meet senior birders who are no longer able to hold binoculars steady or get to the park. I can imagine this might be of some interest to them. At least one elderly birder did a big year on TV, after he could no longer get out so much. This certainly tops that.</p> </article> <nav> <a href="/blog/page/2/">« Previous Entries</a> </nav> </section> <nav> <ul> <li><h2>Info</h2> <ul> <li><a href="/blog/comment-policy/">Comment Policy</a></li> <li><a href="/blog/todo-list/">Todo List</a></li> </ul></li> <li><h2>Archives</h2> <ul> <li><a href='/blog/2007/04/'>April 2007</a></li> <li><a href='/blog/2007/03/'>March 2007</a></li> <li><a href='/blog/2007/02/'>February 2007</a></li> <li><a href='/blog/2007/01/'>January 2007</a></li> </ul> </li> </ul> </nav> <footer> <p>Copyright 2007 Elliotte Rusty Harold</p> </footer> </body> </html> |
現在不再需要 div
了。不再需要自己設置 class 屬性,從標準的元素名就可以推斷出各個部分的意義。這對於音頻瀏覽器、手機瀏覽器和其他非標準瀏覽器尤其重要。
|
除了結構性元素之外,HTML 5 還增加了一些純語義性的塊級元素:
aside
figure
dialog
我在文章和書中一直使用前兩個元素。第三個元素我不經常用,它主要用於書面文本。
aside
元素代表說明、提示、邊欄、引用、附加註釋等,也就是敘述主線之外的內容。例如,在 developerWorks 文章中,常常會看到用表格形式編寫的邊欄,見清單 3。
清單 3. 用 HTML 4 編寫的 developerWorks 邊欄
<table align="right" border="0" cellpadding="0" cellspacing="0" width="40%"> <tbody><tr><td width="10"> <img alt="" src="//www.ibm.com/i/c.gif" height="1" width="10"></td> <td> <table border="1" cellpadding="5" cellspacing="0" width="100%"> <tbody><tr><td bgcolor="#eeeeee"> <p><a name="xf-value"><span class="smalltitle">.xf-value</span></a></p> <p> The <code type="inline">.xf-value</code> selector used here styles the input field value but not its label. This is actually inconsistent with the current CSS3 draft. The example really should use the <code type="inline">::value</code> pseudo-class instead like so: </p> <table border="0" cellpadding="0" cellspacing="0" width="100%"> <tbody><tr><td class="code-outline"> <pre class="displaycode">input::value { width: 20em; } #ccnumber::value { width: 18em } #zip::value { width: 12em } #state::value { width: 3em }</pre> </td></tr></tbody></table><br> <p> However, Firefox doesn't yet support this syntax. </p> </td></tr></table> |
在 HTML 5 中,可以按照更有意義的方式編寫這個邊欄,見清單 4。
清單 4. 用 HTML 5 編寫的 developerWorks 邊欄
<aside> <h3>.xf-value</h3> <p> The <code type="inline">.xf-value</code> selector used here styles the input field value but not its label. This is actually inconsistent with the current CSS3 draft. The example really should use the <code type="inline">::value</code> pseudo-class instead like so: </p> <pre class="displaycode">input::value { width: 20em; } #ccnumber::value { width: 18em } #zip::value { width: 12em } #state::value { width: 3em }</pre> <p> However, Firefox doesn't yet support this syntax. </p> </aside> |
瀏覽器可以決定把這個邊欄放在哪裏(可能需要用一點兒 CSS 代碼)。
figure
元素代表一個塊級圖像,還可以包含說明。例如,在許多 developerWorks 文章中,可以看到清單 5 這樣的標記;其結果見圖 1。
清單 5. 用 HTML 4 編寫的 developerWorks 圖
<a name="fig2"><b>Figure 2. Install Mozilla XForms dialog</b></a><br /> <img alt="A Web site is requesting permission to install the following item: Mozilla XForms 0.7 Unsigned" src="installdialog.jpg" border="0" height="317" hspace="5" vspace="5" width="331" /> <br /> |
圖 1. Install Mozilla XForms dialog
在 HTML 5 中,可以按照更有語義性的方式編寫這個圖,見清單 6。
清單 6. 用 HTML 5 編寫的 developerWorks 圖
<figure id="fig2"> <legend>Figure 2. Install Mozilla XForms dialog</legend> <img alt="A Web site is requesting permission to install the following item: Mozilla XForms 0.7 Unsigned" src="installdialog.jpg" border="0" height="317" hspace="5" vspace="5" width="331" /> </figure> |
最重要的是,瀏覽器(尤其是屏幕閱讀器)可以明確地將圖和說明聯繫在一起。
figure
元素不只可以顯示圖片。還可以使用它給 audio
、video
、iframe
、object
和 embed
元素加說明。
dialog
元素表示幾個人之間的對話。HTML 5 dt
元素可以表示講話者,HTML 5 dd
元素可以表示講話內容。所以,在老式瀏覽器中也可以以合理的方式顯示對話。清單 7 顯示在 Galileo 的 “Dialogue Concerning the Two Chief World Systems” 上的一段著名對話。
清單 7. 用 HTML 5 編寫的 Galilean 對話
<dialog> <dt>Simplicius </dt> <dd>According to the straight line AF, and not according to the curve, such being already excluded for such a use.</dd> <dt>Sagredo </dt> <dd>But I should take neither of them, seeing that the straight line AF runs obliquely. I should draw a line perpendicular to CD, for this would seem to me to be the shortest, as well as being unique among the infinite number of longer and unequal ones which may be drawn from the point A to every other point of the opposite line CD. </dd> <dt>Salviati </dt> <dd><p> Your choice and the reason you adduce for it seem to me most excellent. So now we have it that the first dimension is determined by a straight line; the second (namely, breadth) by another straight line, and not only straight, but at right angles to that which determines the length. Thus we have defined the two dimensions of a surface; that is, length and breadth. </p> <p> But suppose you had to determine a height—for example, how high this platform is from the pavement down below there. Seeing that from any point in the platform we may draw infinite lines, curved or straight, and all of different lengths, to the infinite points of the pavement below, which of all these lines would you make use of? </p> </dd> </dialog> |
對於這個元素的準確語法還有爭議。一些人希望在 dialog
元素中嵌入非對話文本(比如劇本中的舞臺說明),還有人不喜歡擴展 dt
和 dd
元素的作用。儘管在具體語法方面有爭議,但是大多數人都認爲以這樣的語義性方式表達對話是好事情。
|
HTML 4 用 5 個不同的內聯元素表示略有差異的計算機代碼:var
、code
、kbd
、tt
和 samp
。但是,它無法表示時間、數字等基本數值。HTML 5 提供了幾個新的內聯元素來滿足非技術作者的需求。
m
元素表示文本被 “加上標誌”,但是不一定要強調。可以把它想像成書中突出顯示的一節。Google 的緩存頁面就是典型的用例。如果鏈接到一個緩存的副本,搜索詞就被加上標誌。例如,如果搜索 “Egret”,那麼緩存的 Google 頁面可能像下面這樣:
The Great <m>Egret</m> (also known as the American <m>Egret</m>) is a large white wading bird found worldwide. The Great <m>Egret</m> flies with slow wing beats. The scientific name of the Great <m>Egret</m> is <i>Casmerodius albus</i>. |
對於這個元素的名稱當前還有爭議。在規範發佈之前,它可能從 m
改爲 mark
。
time
元素表示一個時間值,比如 5:35 P.M., EST, April 23, 2007。例如:
<p>I am writing this example at <time>5:35 P.M. on April 23rd</time>. </p> |
time
元素可以幫助瀏覽器和其他程序識別出 HTML 頁面中的時間。它不要求對元素內容應用任何特定的格式。但是,每個 time
元素都應該有一個 datetime
屬性,其中包含更適合機器識別的時間值,比如:
<p>I am writing this example at <time datetime="2007-04-23T17:35:00-05:00">5:35 P.M. on April 23rd</time>. </p> |
適合機器讀取的時間值可能對搜索引擎、日曆程序等有幫助。
meter
元素表示指定範圍內的數字值。例如,可以用它表示薪水、投票給 Le Pen 的法國選民的百分比或考試分數。在這裏,我使用 meter
標出 Software Development 2007 上一位 Google 程序員提供的數據:
<p>An entry level programmer in Silicon Valley can expect to start around <meter>$90,000</meter> per year. </p> |
meter
元素幫助瀏覽器和其他客戶機識別 HTML 頁面中的數量。它不要求對元素內容應用任何特定的格式。但是,每個 meter
元素可以有最多 6 個屬性,它們按照更適合機器識別的形式表示這個數量:
value
min
low
high
max
optimum
這些屬性都應該包含一個十進制數字。例如,期末考試的分數可以寫成下面這樣:
<p>Your score was <meter value="88.7" min="0" max="100" low="65" high="96" optimum="100">B+</meter>. </p> |
這表示這個學生的分數是百分制中的 88.7。可能的最低分數是 0,但是實際的最低分數是 65。可能的最高分數是 100,但是實際的最高分數是 96。用戶代理可以用某種數值控件顯示這一信息,也可以在工具提示中顯示額外的數據,但是最常見的情況可能是像其他內聯元素一樣對它應用樣式。
progress
元素表示一個正在進行的過程的狀態,就像圖形用戶界面(GUI)應用程序中的進度條。例如,可以用它表示一個文件已經下載的百分比或者播放電影時的當前位置。下面這個進度控件表示下載已經完成了 33%:
<p>Downloaded: <progress value="1534602" max="4603807">33%</progress> </p> |
value
屬性表示操作的當前狀態。max
屬性表示操作的總量。這個元素指出要下載的數據總量是 4,603,807 字節,已經下載了 1,534,602 字節。
忽略 max
屬性,就可以顯示無限的進度。
在操作進行時,應該使用 JavaScript 語言動態地更新進度條。在靜態情況下,這個元素沒什麼意義。
|
視頻已經在 Web 上廣泛流行了,但是其格式幾乎都是專有的。YouTube 使用 Flash,Microsoft 使用 Windows Media®,Apple 使用 QuickTime。在一種瀏覽器中用來嵌入這些內容的標記在另一種瀏覽器中是無效的。因此,WhatWG 提議引入一個新的 video
元素,用來嵌入任意視頻格式。例如,可以用以下代碼嵌入我的 QuickTime 電影 “a Sora in Prospect Park”:
<video src="http://www.cafeaulait.org/birds/sora.mov" /> |
對於以哪種格式和解碼器作爲首選,仍然有爭議。可能會強力推薦或要求使用 Ogg Theora。還可以可選地支持 QuickTime 等專有格式和 MPEG-4 等受專利限制的格式。實際使用的格式很可能由市場決定,就像是 GIF、JPEG 和 PNG 格式那樣(這些格式通過市場競爭壓倒了 BMP、X-Bitmap 和 JPEG 2000 等競爭者,成爲 img
元素的首選格式)。
還提議引入 audio
元素。例如,可以使用以下代碼給 Web 頁面加上背景音樂:
<audio src="spacemusic.mp3" autoplay="autoplay" loop="20000" /> |
autoplay
屬性指示瀏覽器在裝載頁面後立即開始播放,而不等待明確的用戶請求。音頻循環播放 20,000 次,然後停止(或者在用戶關閉窗口或轉到另一個頁面時停止)。當然,瀏覽器可以(而且應該)允許用戶關閉內嵌的媒體,不應該只按頁面作者的要求去做。
瀏覽器必須支持 WAV 格式,還可以支持 MP3 等其他格式。
因爲老式瀏覽器不支持這些元素,而且它們對於盲人和聾人用戶來說沒有意義,所以 audio
和 video
元素可以包含額外的標記,用來描述音頻和視頻的內容。這對搜索引擎也有幫助。在理想情況下,這些標記是音頻和視頻內容的完整文字版本。例如,清單 8 顯示 John F. Kennedy 的就職演說。
清單 8. 用 HTML 5 編寫的 John F. Kennedy 的就職演說
<audio src="kennedyinauguraladdrees.mp3"> <p> Vice President Johnson, Mr. Speaker, Mr. Chief Justice, President Eisenhower, Vice President Nixon, President Truman, Reverend Clergy, fellow citizens: </p> <p> We observe today not a victory of party, but a celebration of freedom -- symbolizing an end, as well as a beginning -- signifying renewal, as well as change. For I have sworn before you and Almighty God the same solemn oath our forebears prescribed nearly a century and three-quarters ago. </p> <p> The world is very different now. For man holds in his mortal hands the power to abolish all forms of human poverty and all forms of human life. And yet the same revolutionary beliefs for which our forebears fought are still at issue around the globe -- the belief that the rights of man come not from the generosity of the state, but from the hand of God. </p> <p> ... </p> </audio> |
|
HTML 5 也被稱爲 Web Applications 1.0。爲了實現這個目標,增加了幾個爲 Web 頁面提供交互體驗的新元素:
details
datagrid
menu
command
這些元素都可以根據用戶的操作和選擇改變顯示的內容,而不需要從服務器裝載新頁面。
details
元素表示在默認情況下可能不顯示的詳細信息。可選的 legend
元素可以提供詳細信息的摘要。details
元素的用途之一是提供腳註和尾註。例如:
The bill of a Craveri's Murrelet is about 10% thinner than the bill of a Xantus's Murrelet. <details> <legend>[Sibley, 2000]</legend> <p>Sibley, David Allen, The Sibley Guide to Birds, (New York: Chanticleer Press, 2000) p. 247 </p> </details> |
沒有指定具體的顯示方式。瀏覽器可以選用腳註、尾註和工具提示等方式。
每個 details
元素可以有一個 open
屬性。如果設置了這個屬性,那麼詳細信息在最初就顯示出來。如果沒有設置這個屬性,那麼會隱藏它們,直到用戶要求顯示它們。無論是哪種情況,用戶都可以通過單擊一個圖標或其他控件來顯示或隱藏詳細信息。
datagrid
元素提供一個網格控件。可以用它顯示樹、列表和表格,用戶和腳本可以更新這些界面元素。與之相反,傳統的表格主要用來顯示靜態數據。
datagrid
從它的內容(一個 table
、select
或其他 HTML 元素)獲得初始數據。例如,清單 9 中的 datagrid
包含一張成績表。在這個示例中,datagrid
的數據來自一個 table
。更簡單的一維 datagrid
可以從 select
元素獲得數據。如果使用其他 HTML 元素,那麼每個子元素成爲網格中的一行。
清單 9. 成績表 datagrid
<datagrid> <table> <tr><td>Jones</td><td>Allison</td><td>A-</td><td>B+</td><td>A</td></tr> <tr><td>Smith</td><td>Johnny</td><td>A</td><td>C+</td><td>A</td></tr> <tr><td>Willis</td><td>Sydney</td><td>C-</td><td>D</td><td>F</td></tr> <tr><td>Wilson</td><td>Frank</td><td>B-</td><td>B+</td><td>A</td></tr> </table> </datagrid> |
這個元素與常規表格的區別在於,用戶可以選擇行、列和單元格;把行、列和單元格摺疊起來;編輯單元格;刪除行、列和單元格;對網格排序;以及在客戶機瀏覽器中直接進行其他數據操作。可以用 JavaScript 代碼監視更新。Document Object Model(DOM)中增加了 HTMLDataGridElement
接口以支持這個元素(清單 10)。
清單 10. HTMLDataGridElement
interface HTMLDataGridElement : HTMLElement { attribute DataGridDataProvider data; readonly attribute DataGridSelection selection; attribute boolean multiple; attribute boolean disabled; void updateEverything(); void updateRowsChanged(in RowSpecification row, in unsigned long count); void updateRowsInserted(in RowSpecification row, in unsigned long count); void updateRowsRemoved(in RowSpecification row, in unsigned long count); void updateRowChanged(in RowSpecification row); void updateColumnChanged(in unsigned long column); void updateCellChanged(in RowSpecification row, in unsigned long column); }; |
還可以使用 DOM 在網格中動態地裝載數據。也就是說,datagrid
可以不包含那些提供初始數據的子元素。可以用一個 DataGridDataProvider
對象設置它(清單 11)。這樣就可以從數據庫、XmlHttpRequest 或者 JavaScript 代碼能夠訪問的任何資源裝載數據。
清單 11. DataGridDataProvider
interface DataGridDataProvider { void initialize(in HTMLDataGridElement datagrid); unsigned long getRowCount(in RowSpecification row); unsigned long getChildAtPosition(in RowSpecification parentRow, in unsigned long position); unsigned long getColumnCount(); DOMString getCaptionText(in unsigned long column); void getCaptionClasses(in unsigned long column, in DOMTokenList classes); DOMString getRowImage(in RowSpecification row); HTMLMenuElement getRowMenu(in RowSpecification row); void getRowClasses(in RowSpecification row, in DOMTokenList classes); DOMString getCellData(in RowSpecification row, in unsigned long column); void getCellClasses(in RowSpecification row, in unsigned long column, in DOMTokenList classes); void toggleColumnSortState(in unsigned long column); void setCellCheckedState(in RowSpecification row, in unsigned long column, in long state); void cycleCell(in RowSpecification row, in unsigned long column); void editCell(in RowSpecification row, in unsigned long column, in DOMString data); }; |
menu
元素實際上在 HTML 2 中就出現了。在 HTML 4 中廢棄了它,但是 HTML 5 又恢復了它並指定了新的意義。在 HTML 5 中,menu
包含 command
元素,每個 command
元素引發一個操作。例如,清單 12 是一個彈出警告框的菜單。
清單 12. HTML 5 菜單
<menu> <command οnclick="alert('first command')" label="Do 1st Command"/> <command οnclick="alert('second command')" label="Do 2nd Command"/> <command οnclick="alert('third command')" label="Do 3rd Command"/> </menu> |
還可以用 checked="checked"
屬性將命令轉換爲複選框。通過指定 radiogroup
屬性,可以將複選框轉換爲單選按鈕,這個屬性的值是互相排斥的按鈕的組名。
除了簡單的命令列表之外,還可以使用 menu
元素創建工具欄或彈出式上下文菜單,這需要將 type
屬性設置爲 toolbar
或 popup
。例如,清單 13 顯示一個與 WordPress 等 blog 編輯器相似的工具欄。它使用 icon
屬性鏈接到按鈕的圖片。
清單 13. HTML 5 工具欄
<menu type="toolbar"> <command οnclick="insertTag(buttons, 0);" label="strong" icon="bold.gif"/> <command οnclick="insertTag(buttons, 1);" label="em" icon="italic.gif"/> <command οnclick="insertLink(buttons, 2);" label="link" icon="link.gif"/> <command οnclick="insertTag(buttons, 3);" label="b-quote" icon="blockquote.gif"/> <command οnclick="insertTag(buttons, 4);" label="del" icon="del.gif"/> <command οnclick="insertTag(buttons, 5);" label="ins" icon="insert.gif"/> <command οnclick="insertImage(buttons);" label="img" icon="image.gif"/> <command οnclick="insertTag(buttons, 7);" label="ul" icon="bullet.gif"/> <command οnclick="insertTag(buttons, 8);" label="ol" icon="number.gif"/> <command οnclick="insertTag(buttons, 9);" label="li" icon="item.gif"/> <command οnclick="insertTag(buttons, 10);" label="code" icon="code.gif"/> <command οnclick="insertTag(buttons, 11);" label="cite" icon="cite.gif"/> <command οnclick="insertTag(buttons, 12);" label="abbr" icon="abbr.gif"/> <command οnclick="insertTag(buttons, 13);" label="acronym" icon="acronym.gif"/> </menu> |
label
屬性提供菜單的標題。例如,清單 14 顯示一個 Edit 菜單。
清單 14. HTML 5 Edit 菜單
<menu type="popup" label="edit"> <command οnclick="undo()" label="Undo"/> <command οnclick="redo()" label="Redo"/> <command οnclick="cut()" label="Cut"/> <command οnclick="copy()" label="Copy"/> <command οnclick="paste()" label="Paste"/> <command οnclick="delete()" label="Clear"/> </menu> |
菜單可以嵌套在其他菜單中,形成層次化的菜單結構。
|
HTML 5 是未來的 Web 的一部分。它的新元素會產生更乾淨更簡單的代碼,讓頁面更容易理解。Div
和 span
仍然有用處,但是不會像以前那樣頻繁地使用它們了。許多頁面不再需要它們了。
|
儘管目前並非所有瀏覽器都支持這些元素,但這是 HTML 引入大多數新元素之後的普遍情況,比如 img
、table
、object
等等。隨着時間的推移,支持會逐漸完善。瀏覽器會忽略不認識的 HTML 元素,這意味着老式瀏覽器的用戶仍然能夠閱讀 HTML 5 頁面,他們的瀏覽方式與以前一樣。現代瀏覽器的用戶可以獲得更好的用戶體驗,但是沒有人會由於 HTML 5 新元素而妨礙瀏覽。
用 8 年時間等待新特性的出現實在是夠漫長的,尤其是在快速變化的 Web 世界中。在 Web 時代的早期,Netscape、Microsoft 和其他公司幾乎每週都會引入新元素,HTML 5 終於也爲我們提供了一些令人興奮的東西。同時,它以一種謹慎得多的方式定義這些元素,讓我們可以放心地使用它們。前景是光明的。