高级文本功能
HTML 中有许多其他元素用于定义文本语义,我们没有在强调和重要性文章中提及。本文描述的元素鲜为人知,但仍然很有用(这绝不是一个完整的列表)。在这里你将学习如何标记引用、计算机代码及其他相关文本、上标和下标、联系信息等等。
预备知识 | 具备 基本 HTML 语法 中涵盖的基本 HTML 知识。文本级语义,例如 标题和段落 以及 列表。 |
---|---|
学习成果 |
|
引用
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.org.cn/en-US/docs/Web/HTML/Reference/Elements/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.org.cn/en-US/docs/Web/HTML/Reference/Elements/q">
intended for short quotations that don't require paragraph breaks.
</q>
</p>
浏览器默认样式会将其渲染为用引号引起来的普通文本,以表示引用,如下所示
引文
cite
属性的内容听起来很有用,但不幸的是,浏览器、屏幕阅读器等并没有对其进行太多处理。如果没有使用 JavaScript 或 CSS 编写自己的解决方案,就无法让浏览器显示cite
的内容。如果您想在页面上提供引用的来源,您需要通过链接或其他适当的方式在文本中提供它。
有一个<cite>
元素,但它旨在包含被引用资源的标题,例如书名。但是,没有理由不能以某种方式将<cite>
中的文本链接到引用源
<p>
According to the
<a href="/en-US/docs/Web/HTML/Reference/Elements/blockquote">
<cite>MDN blockquote page</cite></a>:
</p>
<blockquote
cite="https://mdn.org.cn/en-US/docs/Web/HTML/Reference/Elements/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.org.cn/en-US/docs/Web/HTML/Reference/Elements/q">
intended for short quotations that don't require paragraph breaks.
</q>
— <a href="/en-US/docs/Web/HTML/Reference/Elements/q"><cite>MDN q page</cite></a>.
</p>
引文默认以斜体字体显示。
谁说的?块引用练习
又到了任务时间!在此示例中,我们希望您
- 单击下面代码块中的**“播放”**以在 MDN Playground 中编辑示例。
- 将中间段落转换为块引用,其中包含一个
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
。
如果您犯了错误,可以使用 MDN Playground 中的重置按钮清除您的工作。如果您真的卡住了,可以在代码块下方查看解决方案。
<p>Hello and welcome to my motivation page. As Confucius' quotes site says:</p>
<p>It does not matter how slowly you go as long as you do not stop.</p>
<p>
I also love the concept of positive thinking, and The Need To Eliminate
Negative Self Talk (as mentioned in Affirmations for Positive Thinking.)
</p>
点击此处显示解决方案
您完成的 HTML 应该看起来像这样
<p>
Hello and welcome to my motivation page. As
<a href="http://www.brainyquote.com/quotes/authors/c/confucius.html"
><cite>Confucius' quotes site</cite></a
>
says:
</p>
<blockquote cite="http://www.brainyquote.com/quotes/authors/c/confucius.html">
<p>It does not matter how slowly you go as long as you do not stop.</p>
</blockquote>
<p>
I also love the concept of positive thinking, and
<q cite="http://example.com/affirmationsforpositivethinking"
>The Need To Eliminate Negative Self Talk</q
>
(as mentioned in
<a href="http://example.com/affirmationsforpositivethinking"
><cite>Affirmations for Positive Thinking</cite></a
>.)
</p>
缩写
在浏览网页时,您会遇到的另一个相当常见的元素是<abbr>
——它用于包裹缩写词或首字母缩略词。在包含任何一种情况时,首次使用时以纯文本形式提供该术语的完整扩展,并使用<abbr>
标记该缩写。这为用户代理提供了如何宣布/显示内容的提示,同时告知所有用户该缩写的含义。
如果除了缩写之外再提供扩展意义不大,并且该缩写或首字母缩略词是一个相当短的术语,则将该术语的完整扩展作为title
属性的值
缩写示例
让我们看一个例子。
<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>
。
让我们标记一个缩写
对于此学习任务,我们希望您标记一个缩写。
- 单击下面代码块中的**“播放”**以在 MDN Playground 中编辑示例。
- 使用适当的 HTML 标记包含的缩写。您也可以随意将其替换为您自己的缩写,并尝试标记它。
如果您犯了错误,可以使用 MDN Playground 中的重置按钮清除您的工作。如果您真的卡住了,可以在代码块下方查看解决方案。
<p>NASA sure does some exciting work.</p>
<p>The new user interface design LGTM!</p>
点击此处显示解决方案
您完成的 HTML 应该类似于以下代码片段
<p>
<abbr>NASA</abbr> (the National Aeronautics and Space Administration) sure
does some exciting work.
</p>
<p>The new user interface design <abbr title="Looks good to me">LGTM</abbr>!</p>
- 可以说,NASA 应该在首次提及时在文本中展开,因为这对每个人来说都是文本中可用的有用信息。
- 另一方面,像“LGTM”这样的首字母缩略词纯粹是为了节省空间和时间而编写的,因此再写出来就没有意义了,因此将其扩展放在
title
属性中。在实际应用程序中,您可能不会手动执行此操作——您会使用某种脚本自动添加已知术语。
标记联系方式
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>
上标和下标
在标记日期、化学式和数学方程等项目时,您偶尔需要使用上标和下标,以便它们具有正确的含义。<sup>
和<sub>
元素处理这项工作。例如
<p>My birthday is on the 25<sup>th</sup> of May 2001.</p>
<p>
Caffeine's chemical formula is
C<sub>8</sub>H<sub>10</sub>N<sub>4</sub>O<sub>2</sub>.
</p>
<p>If x<sup>2</sup> is 9, x must equal 3 or -3.</p>
此代码的输出如下所示
表示计算机代码
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 文本功能的信息的理解和掌握程度。