高階文字格式化
HTML 中還有許多其他用於格式化文字的元素,我們在HTML 文字基礎文章中沒有提到。本文中描述的元素不太為人所知,但仍然有用(而且這絕不是完整列表)。在這裡,您將學習如何標記引用、描述列表、計算機程式碼和其他相關文字、下標和上標、聯絡資訊等等。
描述列表
在 HTML 文字基礎中,我們介紹瞭如何在 HTML 中標記基本列表,並且我們提到了您偶爾會遇到的第三種列表型別——**描述列表**。這些列表的目的是標記一組專案及其關聯的描述,例如術語和定義,或問題和答案。讓我們看一個術語和定義的示例
soliloquy In drama, where a character speaks to themselves, representing their inner thoughts or feelings and in the process relaying them to the audience (but not to other characters.) monologue In drama, where a character speaks their thoughts out loud to share them with the audience and any other characters present. aside In drama, where a character shares a comment only with the audience for humorous or dramatic effect. This is usually a feeling, thought or piece of additional background information
描述列表使用與其他列表型別不同的包裝器——<dl>;此外,每個術語都包裝在<dt>(描述術語)元素中,每個描述都包裝在<dd>(描述定義)元素中。
描述列表示例
讓我們完成我們示例的標記
<dl>
<dt>soliloquy</dt>
<dd>
In drama, where a character speaks to themselves, representing their inner
thoughts or feelings and in the process relaying them to the audience (but
not to other characters.)
</dd>
<dt>monologue</dt>
<dd>
In drama, where a character speaks their thoughts out loud to share them
with the audience and any other characters present.
</dd>
<dt>aside</dt>
<dd>
In drama, where a character shares a comment only with the audience for
humorous or dramatic effect. This is usually a feeling, thought, or piece of
additional background information.
</dd>
</dl>
瀏覽器預設樣式會將描述列表的描述相對於術語略微縮排。
一個術語的多個描述
請注意,允許一個術語具有多個描述,例如
<dl>
<dt>aside</dt>
<dd>
In drama, where a character shares a comment only with the audience for
humorous or dramatic effect. This is usually a feeling, thought, or piece of
additional background information.
</dd>
<dd>
In writing, a section of content that is related to the current topic, but
doesn't fit directly into the main flow of content so is presented nearby
(often in a box off to the side.)
</dd>
</dl>
主動學習:標記一組定義
現在輪到您嘗試描述列表了;在輸入欄位中的原始文字中新增元素,使其在輸出欄位中顯示為描述列表。如果您願意,可以使用您自己的術語和描述。
如果您犯了錯誤,可以使用重置按鈕隨時重置。如果您真的卡住了,請按顯示解決方案按鈕檢視答案。
引用
HTML 還提供了用於標記引用的功能;您使用哪個元素取決於您是在標記塊引用還是內聯引用。
塊引用
如果一段塊級內容(無論是段落、多個段落、列表等)是從其他地方引用過來的,則應將其包裝在<blockquote>元素中以表示這一點,並在cite屬性中包含指向引用來源的 URL。例如,以下標記取自 MDN 的<blockquote>元素頁面
<p>
The <strong>HTML <code><blockquote></code> Element</strong> (or
<em>HTML Block Quotation Element</em>) indicates that the enclosed text is an
extended quotation.
</p>
要將其轉換為塊引用,我們只需執行以下操作
<p>Here is a blockquote:</p>
<blockquote
cite="https://mdn.club.tw/en-US/docs/Web/HTML/Element/blockquote">
<p>
The <strong>HTML <code><blockquote></code> Element</strong> (or
<em>HTML Block Quotation Element</em>) indicates that the enclosed text is
an extended quotation.
</p>
</blockquote>
瀏覽器預設樣式會將其呈現為縮排的段落,以指示它是一個引用;引用上方的段落是為了演示這一點。
內聯引用
內聯引用的工作方式完全相同,只是它們使用<q>元素。例如,以下標記包含來自 MDN 的<q>頁面的引用
<p>
The quote element — <code><q></code> — is
<q cite="https://mdn.club.tw/en-US/docs/Web/HTML/Element/q">
intended for short quotations that don't require paragraph breaks.
</q>
</p>
瀏覽器預設樣式會將其呈現為用引號括起來的普通文字,以指示引用,如下所示
引用
儘管`cite` 屬性的內容看起來很有用,但不幸的是,瀏覽器、螢幕閱讀器等並沒有真正利用它。無法讓瀏覽器顯示 cite 的內容,除非使用 JavaScript 或 CSS 編寫自己的解決方案。如果你想在頁面上提供引文的來源,你需要透過連結或其他合適的方式在文字中提供。
有一個`<cite>` 元素,但它用於包含被引用資源的標題,例如書籍名稱。但是,沒有任何理由阻止你以某種方式將 <cite> 內部的文字連結到引文來源。
<p>
According to the
<a href="/en-US/docs/Web/HTML/Element/blockquote">
<cite>MDN blockquote page</cite></a>:
</p>
<blockquote
cite="https://mdn.club.tw/en-US/docs/Web/HTML/Element/blockquote">
<p>
The <strong>HTML <code><blockquote></code> Element</strong> (or
<em>HTML Block Quotation Element</em>) indicates that the enclosed text is
an extended quotation.
</p>
</blockquote>
<p>
The quote element — <code><q></code> — is
<q cite="https://mdn.club.tw/en-US/docs/Web/HTML/Element/q">
intended for short quotations that don't require paragraph breaks.
</q>
— <a href="/en-US/docs/Web/HTML/Element/q"><cite>MDN q page</cite></a>.
</p>
預設情況下,引用以斜體顯示。
主動學習:誰說的?
是時候進行另一個主動學習示例了!在這個示例中,我們希望你
- 將中間段落轉換為一個包含
cite屬性的塊引用。 - 將第三段落中的“The Need To Eliminate Negative Self Talk”轉換為內聯引用,幷包含
cite屬性。 - 將每個來源的標題用
<cite>標籤包裹起來,並將每個標題轉換為指向該來源的連結。
你需要引用的來源是
- 孔子語錄的來源:
http://www.brainyquote.com/quotes/authors/c/confucius.html - “The Need To Eliminate Negative Self Talk”的來源:
http://example.com/affirmationsforpositivethinking。
如果您犯了錯誤,可以使用重置按鈕隨時重置。如果您真的卡住了,請按顯示解決方案按鈕檢視答案。
縮寫
縮寫示例
讓我們看一個例子。
<p>
We use <abbr>HTML</abbr>, Hypertext Markup Language, to structure our web
documents.
</p>
<p>
I think <abbr title="Reverend">Rev.</abbr> Green did it in the kitchen with
the chainsaw.
</p>
這些將看起來像這樣
注意:早期版本的 HTML 也支援`<acronym>` 元素,但它已從 HTML 規範中移除,轉而使用 <abbr> 來表示縮寫和首字母縮略詞。不應使用 <acronym>。
主動學習:標記縮寫
對於這個簡單的主動學習作業,我們希望你標記一個縮寫。你可以使用下面的示例,或者用你自己的示例替換它。
標記聯絡資訊
HTML 有一個用於標記聯絡資訊的元素`<address>`。它用於包裹你的聯絡資訊,例如
<address>Chris Mills, Manchester, The Grim North, UK</address>
它還可以包含更復雜的標記和其他形式的聯絡資訊,例如
<address>
<p>
Chris Mills<br />
Manchester<br />
The Grim North<br />
UK
</p>
<ul>
<li>Tel: 01234 567 890</li>
<li>Email: me@grim-north.co.uk</li>
</ul>
</address>
請注意,如果連結的頁面包含聯絡資訊,以下內容也是可以的。
<address>
Page written by <a href="../authors/chris-mills/">Chris Mills</a>.
</address>
上標和下標
表示計算機程式碼
有許多元素可用於使用 HTML 標記計算機程式碼
<code>:用於標記通用的計算機程式碼片段。<pre>:用於保留空白字元(通常是程式碼塊)——如果你在文字中使用縮排或多餘的空白字元,瀏覽器將忽略它們,你將不會在渲染的頁面上看到它們。但是,如果你將文字用<pre></pre>標籤包裹起來,你的空白字元將與你在文字編輯器中看到的一樣進行渲染。<var>:專門用於標記變數名稱。<kbd>:用於標記輸入到計算機中的鍵盤(和其他型別)輸入。<samp>:用於標記計算機程式的輸出。
讓我們看看這些元素的示例以及它們如何用於表示計算機程式碼。如果你想檢視完整的檔案,請檢視`other-semantics.html` 示例檔案。你可以下載該檔案並在瀏覽器中開啟它以自行檢視,但這裡有一段程式碼片段
<pre><code>const para = document.querySelector('p');
para.onclick = function() {
alert('Owww, stop poking me!');
}</code></pre>
<p>
You shouldn't use presentational elements like <code><font></code> and
<code><center></code>.
</p>
<p>
In the above JavaScript example, <var>para</var> represents a paragraph
element.
</p>
<p>Select all the text with <kbd>Ctrl</kbd>/<kbd>Cmd</kbd> + <kbd>A</kbd>.</p>
<pre>$ <kbd>ping mozilla.org</kbd>
<samp>PING mozilla.org (63.245.215.20): 56 data bytes
64 bytes from 63.245.215.20: icmp_seq=0 ttl=40 time=158.233 ms</samp></pre>
以上程式碼將如下所示
標記時間和日期
HTML 還提供`<time>` 元素,用於以機器可讀的格式標記時間和日期。例如
<time datetime="2016-01-20">20 January 2016</time>
這有什麼用?嗯,人類記錄日期的方式有很多種。上述日期可以寫成
- 20 January 2016
- 20th January 2016
- Jan 20 2016
- 20/01/16
- 01/20/16
- The 20th of next month
- 20e Janvier 2016
- 2016 年 1 月 20 日
- 等等
但這些不同的形式不容易被計算機識別——如果你想自動獲取頁面上所有事件的日期並將其插入日曆中怎麼辦?`<time>` 元素允許你為此目的附加一個明確的、機器可讀的時間/日期。
上面的基本示例只提供了一個簡單的機器可讀日期,但還有許多其他選項是可能的,例如
<!-- Standard simple date -->
<time datetime="2016-01-20">20 January 2016</time>
<!-- Just year and month -->
<time datetime="2016-01">January 2016</time>
<!-- Just month and day -->
<time datetime="01-20">20 January</time>
<!-- Just time, hours and minutes -->
<time datetime="19:30">19:30</time>
<!-- You can do seconds and milliseconds too! -->
<time datetime="19:30:01.856">19:30:01.856</time>
<!-- Date and time -->
<time datetime="2016-01-20T19:30">7.30pm, 20 January 2016</time>
<!-- Date and time with timezone offset -->
<time datetime="2016-01-20T19:30+01:00">
7.30pm, 20 January 2016 is 8.30pm in France
</time>
<!-- Calling out a specific week number -->
<time datetime="2016-W04">The fourth week of 2016</time>
測試你的技能!
你已經到達了本文的結尾,但你能記住最重要的資訊嗎?在繼續之前,你可以找到一些進一步的測試來驗證你是否保留了這些資訊——請參閱`測試你的技能:高階 HTML 文字`。
總結
這標誌著我們對 HTML 文字語義的研究結束。請記住,你在本課程中學到的內容並非 HTML 文字元素的詳盡列表——我們希望嘗試涵蓋基本內容,以及你在實際應用中會看到的一些更常見的元素,或者至少你可能會覺得有趣的元素。要查詢更多 HTML 元素,你可以檢視我們的`HTML 元素參考`(`內聯文字語義` 部分將是一個很好的起點)。在下一篇文章中,我們將瞭解用於`構建 HTML 文件的不同部分`的 HTML 元素。