title

title 全局属性 包含表示与其所属元素相关的咨询信息的文本。

¥The title global attribute contains text representing advisory information related to the element it belongs to.

Try it

title 属性的主要用途是为辅助技术标记 <iframe> 元素。

¥The main use of the title attribute is to label <iframe> elements for assistive technology.

title 属性也可用于标记 数据表 中的控件。

¥The title attribute may also be used to label controls in data tables.

title 属性添加到 <link rel="stylesheet"> 后,会创建备用样式表。当使用 <link rel="alternate"> 定义替代样式表时,该属性是必需的,并且必须设置为非空字符串。

¥The title attribute, when added to <link rel="stylesheet">, creates an alternate stylesheet. When defining an alternative style sheet with <link rel="alternate"> the attribute is required and must be set to a non-empty string.

如果包含在 <abbr> 开始标签中,则 title 必须是缩写词或首字母缩略词的完整扩展。如果可能,请在首次使用时以纯文本形式提供缩写词或首字母缩略词的扩展,而不是使用 title,并使用 <abbr> 来标记缩写词。这使得所有用户都知道缩写词或首字母缩写词缩写的名称或术语,同时向用户代理提供有关如何宣布内容的提示。

¥If included on the <abbr> opening tag, the title must be a full expansion of the abbreviation or acronym. Instead of using title, when possible, provide an expansion of the abbreviation or acronym in plain text on first use, using the <abbr> to mark up the abbreviation. This enables all users know what name or term the abbreviation or acronym shortens while providing a hint to user agents on how to announce the content.

虽然 title 可用于为 <input> 元素提供以编程方式关联的标签,但这不是一个好的做法。请改用 <label>

¥While title can be used to provide a programmatically associated label for an <input> element, this is not good practice. Use a <label> instead.

多行标题

¥Multiline titles

title 属性可能包含多行。每个 U+000A LINE FEED (LF) 字符代表一个换行符。必须小心,因为这意味着以下内容将跨两行呈现:

¥The title attribute may contain several lines. Each U+000A LINE FEED (LF) character represents a line break. Some caution must be taken, as this means the following renders across two lines:

HTML

html
<p>
  Newlines in <code>title</code> should be taken into account. This
  <span
    title="This is a
multiline title">
    example span
  </span>
  has a title a attribute with a newline.
</p>
<hr />
<pre id="output"></pre>

JavaScript

我们可以查询 title 属性并将其显示在空的 <pre> 元素中,如下所示:

¥We can query the title attribute and display it in the empty <pre> element as follows:

js
const span = document.querySelector("span");
const output = document.querySelector("#output");
output.textContent = span.title;

结果

¥Result

标题属性继承

¥Title attribute inheritance

如果一个元素没有 title 属性,那么它会从其父节点继承该属性,而父节点又可能会从其父节点继承该属性,依此类推。

¥If an element has no title attribute, then it inherits it from its parent node, which in turn may inherit it from its parent, and so on.

如果此属性设置为空字符串,则意味着其祖级的 title 不相关,并且不应在此元素的工具提示中使用。

¥If this attribute is set to the empty string, it means its ancestors' titles are irrelevant and shouldn't be used in the tooltip for this element.

HTML

html
<div title="CoolTip">
  <p>Hovering here will show "CoolTip".</p>
  <p title="">Hovering here will show nothing.</p>
</div>

结果

¥Result

无障碍问题

¥Accessibility concerns

使用 title 属性对于以下情况来说是非常有问题的:

¥Use of the title attribute is highly problematic for:

  • 使用纯触摸设备的人
  • 人们使用键盘进行导航
  • 使用屏幕阅读器或放大器等辅助技术进行导航的人
  • 患有精细运动控制障碍的人
  • 有认知问题的人

这是由于浏览器支持不一致,再加上浏览器渲染页面的附加辅助技术解析造成的。如果想要 tooltip 效果,最好是 使用更容易理解的技术,可以用上面的浏览方式访问。

¥This is due to inconsistent browser support, compounded by the additional assistive technology parsing of the browser-rendered page. If a tooltip effect is desired, it is better to use a more accessible technique that can be accessed with the above browsing methods.

规范

Specification
HTML Standard
# the-title-attribute

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看

¥See also