HTML标签 <a> 用于创建超链接,即用户点击后可以跳转到另一个网页或指定位置。以下是 <a> 标签的常用属性:
href: 指定链接的目标地址,可以是一个URL或一个本地文档的路径。target: 指定链接打开的方式,常用的取值有_blank(在新窗口打开)、_self(在当前窗口打开)、_parent(在父窗口打开)和_top(在顶层窗口打开)。title: 鼠标悬停在链接上时显示的文本。download: 指定链接下载资源时的文件名。
示例代码:
<a href="https://www.example.com" target="_blank" title="Visit Example">Visit Example</a>
在上面的示例中,用户点击 "Visit Example" 链接后会在新窗口打开 https://www.example.com 网页,并在鼠标悬停时显示 "Visit Example"。
除了常规的文本链接,<a> 标签还可以用于创建锚点链接,即在同一页面内跳转到指定位置。这时可以结合使用 id 属性和 href 属性来实现:
<a href="#section2">Jump to Section 2</a> <h2 id="section2">Section 2</h2> <p>This is the content of Section 2.</p>
在这个示例中,点击 "Jump to Section 2" 链接时会跳转到页面中的 id="section2" 的位置,即 Section 2 的标题处。
除了超链接外,<a> 标签还可以用于创建邮箱链接和电话链接,分别使用 mailto: 和 tel: 协议:
<a href="mailto:info@example.com">Email Us</a> <a href="tel:123-456-7890">Call Us</a>
在这两个示例中,点击 "Email Us" 链接会打开默认邮箱客户端并填入收件人地址为 info@example.com,点击 "Call Us" 链接会拨打电话号码 123-456-7890。
这就是 <a> 标签的基本用法和常用属性。通过合理使用超链接,可以为用户提供更好的导航和交互体验。
| 属性 | 值 | 描述 |
|---|---|---|
| charset | char_encoding | HTML5 不支持。规定目标 URL 的字符编码。 |
| coords | coordinates | HTML5 不支持。规定链接的坐标。 |
| download | filename | 指定下载链接 |
| href | URL | 规定链接的目标 URL。 |
| hreflang | language_code | 规定目标 URL 的基准语言。仅在 href 属性存在时使用。 |
| media | media_query | 规定目标 URL 的媒介类型。默认值:all。仅在 href 属性存在时使用。 |
| name | section_name | HTML5 不支持。规定锚的名称。 |
| rel | alternate author bookmark help license next nofollow noreferrer prefetch prev search tag | 规定当前文档与目标 URL 之间的关系在 href 属性存在时使用。 |
| rev | text | HTML5 不支持。规定目标 URL 与当前文档之间的关系。 |
| shape | default rect circle poly | HTML5 不支持。规定链接的形状。 |
| target | _blank _parent _self _top framename | 规定在何处打开目标 URL。仅在 href 属性存在时使用。_blank:新窗口打 _parent:在父窗口中打开链接。_self:默认,当前页面跳转。_top:在当前窗体打开链接,并替换当前的整个窗体(框架页)。 |
| type | MIME_type | 规定目标 URL 的 MIME 类型。仅在 href 属性存在时使用。注:MIME = Multipurpose Internet Mail Extensions。 |