试一试
<p>
A <dfn id="def-validator">validator</dfn> is a program that checks for syntax
errors in code or documents.
</p>
dfn {
/* Add your styles here */
}
属性
此元素的属性包括全局属性。
title
属性有特殊含义,如下文所述。
用法说明
使用 <dfn>
元素有一些不太明显但很有用的方面。我们在这里进行探讨。
指定被定义的术语
被定义的术语按照以下规则进行识别:
- 如果
<dfn>
元素有一个title
属性,则title
属性的值被认为是将被定义的术语。该元素仍然必须在其内部包含文本,但该文本可以是缩写(可能使用<abbr>
)或该术语的另一种形式。 - 如果
<dfn>
包含一个子元素且没有自己的文本内容,并且该子元素是一个带有title
属性的<abbr>
元素,那么<abbr>
元素的title
的确切值就是被定义的术语。 - 否则,
<dfn>
元素的文本内容就是被定义的术语。这在 下面的第一个示例 中有展示。
注意: 如果 <dfn>
元素有一个 title
属性,它必须包含被定义的术语,而不包含任何其他文本。
链接到 <dfn>
元素
如果您在 <dfn>
元素上包含一个 id
属性,您就可以使用 <a>
元素链接到它。这样的链接应该是对该术语的使用,目的是让读者能够通过点击术语的链接,快速导航到该术语的定义,如果他们还不熟悉的话。
这在下面的 定义链接 中的示例中有所展示。
示例
让我们来看一些各种使用场景的示例。
术语的基本标识
此示例使用一个普通的 <dfn>
元素来标识定义中的术语位置。
HTML
html
<p>
The <strong>HTML Definition element (<dfn><dfn></dfn>)</strong> is used
to indicate the term being defined within the context of a definition phrase
or sentence.
</p>
由于 <dfn>
元素没有 title
,因此 <dfn>
元素本身的文本内容被用作被定义的术语。
结果
定义链接
要为定义添加链接,您需要像往常一样使用 <a>
元素来创建链接。
HTML
html
<p>
The
<strong>HTML Definition element (<dfn id="definition-dfn"><dfn></dfn>)</strong>
is used to indicate the term being defined within the context of a definition
phrase or sentence.
</p>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Graece donan, Latine
voluptatem vocant. Confecta res esset. Duo Reges: constructio interrete.
Scrupulum, inquam, abeunti;
</p>
<p>
Because of all of that, we decided to use the
<code><a href="#definition-dfn"><dfn></a></code> element for this
project.
</p>
在这里,我们看到了定义 — 现在带有一个 id
属性 "definition-dfn"
,可用作链接目标。稍后,将使用 <a>
元素创建链接,并将其 href
属性设置为 "#definition-dfn"
,以建立回定义链接。
结果
将缩写和定义结合使用
在某些情况下,您可能希望在使用缩写来定义术语。可以通过像这样联合使用 <dfn>
和 <abbr>
元素来实现:
HTML
html
<p>
The <dfn><abbr title="Hubble Space Telescope">HST</abbr></dfn> is among the
most productive scientific instruments ever constructed. It has been in orbit
for over 20 years, scanning the sky and returning data and photographs of
unprecedented quality and detail.
</p>
<p>
Indeed, the <abbr title="Hubble Space Telescope">HST</abbr> has arguably done
more to advance science than any device ever built.
</p>
请注意嵌套在 <dfn>
中的 <abbr>
元素。前者表明该术语是一个缩写(“HST”),并在其 title
属性中指定了完整的术语(“Hubble Space Telescope”)。后者表示缩写的术语代表一个正在被定义的术语。
结果
技术摘要
内容类别 | Flow content、phrasing content、palpable content。 |
---|---|
允许内容 |
Phrasing content,但 <dfn> 元素不能是后代。 |
标签省略 | 无,起始标签和结束标签都必须存在。 |
允许父级 | 任何接受短语内容的元素。 |
隐式 ARIA 角色 | term |
允许的 ARIA 角色 | 任意 |
DOM 接口 | HTMLElement |
规范
规范 |
---|
HTML # the-dfn-element |
浏览器兼容性
加载中…