链接是HTML的核心特性之一,它将不同的网页和资源连接在一起,形成互联网的基础。
东巴文(db-w.cn) 认为:链接是互联网的灵魂,合理使用链接可以提升用户体验和SEO效果。
<a>标签<a>标签(anchor,锚)用于创建超链接。
<a href="https://db-w.cn">访问东巴文</a>
东巴文说明:
href属性:指定链接目标URL东巴文链接属性:
| 属性 | 说明 | 示例 |
|---|---|---|
href |
链接目标URL | href="https://db-w.cn" |
target |
打开方式 | target="_blank" |
rel |
链接关系 | rel="noopener" |
title |
提示文本 | title="点击访问" |
download |
下载文件 | download="filename" |
hreflang |
链接语言 | hreflang="zh-CN" |
type |
MIME类型 | type="text/html" |
绝对链接包含完整的URL地址。
<!-- 完整URL -->
<a href="https://db-w.cn/courses/html">HTML教程</a>
<!-- 协议相对URL -->
<a href="//db-w.cn/courses/css">CSS教程</a>
<!-- 带端口的URL -->
<a href="https://localhost:8080">本地服务器</a>
东巴文说明:
相对链接相对于当前文档的位置。
<!-- 同级目录 -->
<a href="page.html">同级页面</a>
<!-- 下级目录 -->
<a href="courses/html.html">HTML教程</a>
<!-- 上级目录 -->
<a href="../index.html">返回上级</a>
<!-- 根目录 -->
<a href="/index.html">首页</a>
东巴文相对路径:
| 路径 | 说明 | 示例 |
|---|---|---|
file.html |
同级文件 | href="page.html" |
dir/file.html |
下级目录文件 | href="courses/html.html" |
../file.html |
上级目录文件 | href="../index.html" |
/file.html |
根目录文件 | href="/index.html" |
东巴文说明:
锚点链接用于页面内跳转。
<!-- 定义锚点 -->
<h2 id="chapter1">第一章</h2>
<p>这是第一章的内容...</p>
<h2 id="chapter2">第二章</h2>
<p>这是第二章的内容...</p>
<!-- 跳转到锚点 -->
<a href="#chapter1">跳转到第一章</a>
<a href="#chapter2">跳转到第二章</a>
<!-- 返回顶部 -->
<a href="#">返回顶部</a>
东巴文说明:
id属性定义锚点#加id值跳转href="#"跳转到页面顶部东巴文锚点导航示例:
<nav>
<ul>
<li><a href="#intro">简介</a></li>
<li><a href="#features">特性</a></li>
<li><a href="#usage">使用方法</a></li>
<li><a href="#faq">常见问题</a></li>
</ul>
</nav>
<article>
<section id="intro">
<h2>简介</h2>
<p>东巴文(db-w.cn)是一个编程学习平台...</p>
</section>
<section id="features">
<h2>特性</h2>
<p>东巴文具有以下特性...</p>
</section>
<section id="usage">
<h2>使用方法</h2>
<p>如何使用东巴文...</p>
</section>
<section id="faq">
<h2>常见问题</h2>
<p>常见问题解答...</p>
</section>
</article>
邮件链接用于发送电子邮件。
<!-- 基本邮件链接 -->
<a href="mailto:support@db-w.cn">联系我们</a>
<!-- 带主题的邮件链接 -->
<a href="mailto:support@db-w.cn?subject=咨询">咨询问题</a>
<!-- 带主题和正文的邮件链接 -->
<a href="mailto:support@db-w.cn?subject=咨询&body=您好">发送邮件</a>
<!-- 多个收件人 -->
<a href="mailto:support@db-w.cn,admin@db-w.cn">联系团队</a>
<!-- 带抄送和密送 -->
<a href="mailto:support@db-w.cn?cc=admin@db-w.cn&bcc=manager@db-w.cn">
联系我们
</a>
东巴文邮件链接参数:
| 参数 | 说明 | 示例 |
|---|---|---|
subject |
邮件主题 | subject=咨询 |
body |
邮件正文 | body=您好 |
cc |
抄送 | cc=admin@db-w.cn |
bcc |
密送 | bcc=manager@db-w.cn |
电话链接用于拨打电话。
<!-- 基本电话链接 -->
<a href="tel:+8612345678900">拨打电话</a>
<!-- 短格式 -->
<a href="tel:12345678900">12345678900</a>
<!-- 带分机号 -->
<a href="tel:+8612345678900,123">拨打电话(分机123)</a>
东巴文说明:
文件下载链接用于下载文件。
<!-- 基本下载链接 -->
<a href="files/document.pdf">下载PDF</a>
<!-- 指定下载文件名 -->
<a href="files/document.pdf" download="教程文档.pdf">
下载PDF
</a>
<!-- 下载图片 -->
<a href="images/photo.jpg" download="照片.jpg">
下载图片
</a>
东巴文说明:
download属性指定下载文件名target属性target属性指定链接打开方式。
东巴文target属性值:
| 属性值 | 说明 | 使用场景 |
|---|---|---|
_self |
当前窗口(默认) | 站内链接 |
_blank |
新窗口 | 站外链接 |
_parent |
父框架 | 框架页面 |
_top |
顶层窗口 | 框架页面 |
<!-- 当前窗口打开 -->
<a href="page.html" target="_self">当前窗口打开</a>
<!-- 新窗口打开 -->
<a href="https://example.com" target="_blank">新窗口打开</a>
<!-- 父框架打开 -->
<a href="page.html" target="_parent">父框架打开</a>
<!-- 顶层窗口打开 -->
<a href="page.html" target="_top">顶层窗口打开</a>
使用target="_blank"时,应添加rel="noopener"或rel="noreferrer"。
<!-- ✅ 推荐:安全的外部链接 -->
<a href="https://example.com" target="_blank" rel="noopener noreferrer">
外部链接
</a>
<!-- ❌ 不推荐:不安全的外部链接 -->
<a href="https://example.com" target="_blank">
外部链接
</a>
东巴文说明:
noopener:防止新页面访问原页面的window.openernoreferrer:不发送引用页信息rel属性rel属性定义当前文档与链接文档的关系。
东巴文常用rel值:
| rel值 | 说明 | 使用场景 |
|---|---|---|
noopener |
安全打开 | target="_blank" |
noreferrer |
不发送引用信息 | 隐私保护 |
nofollow |
不传递权重 | 广告链接 |
noopener noreferrer |
组合使用 | 外部链接 |
alternate |
替代版本 | 多语言页面 |
author |
作者信息 | 作者页面 |
bookmark |
书签 | 永久链接 |
external |
外部链接 | 标识外部链接 |
help |
帮助文档 | 帮助页面 |
license |
许可证 | 版权信息 |
next |
下一页 | 分页导航 |
prev |
上一页 | 分页导航 |
tag |
标签 | 标签页面 |
<!-- 外部链接 -->
<a href="https://example.com" rel="external">外部网站</a>
<!-- 广告链接 -->
<a href="https://ad.com" rel="nofollow">广告</a>
<!-- 多语言版本 -->
<link rel="alternate" hreflang="en" href="https://db-w.cn/en/">
<!-- 分页导航 -->
<a href="page1.html" rel="prev">上一页</a>
<a href="page3.html" rel="next">下一页</a>
浏览器默认链接样式:
<style>
/* 基础样式 */
a {
color: #0066cc;
text-decoration: none;
}
/* 悬停样式 */
a:hover {
color: #ff6600;
text-decoration: underline;
}
/* 访问后样式 */
a:visited {
color: #660099;
}
/* 激活样式 */
a:active {
color: #ff0000;
}
/* 焦点样式 */
a:focus {
outline: 2px solid #0066cc;
}
/* 导航链接 */
.nav-link {
display: inline-block;
padding: 10px 20px;
background-color: #f0f0f0;
color: #333;
text-decoration: none;
border-radius: 5px;
}
.nav-link:hover {
background-color: #0066cc;
color: #fff;
}
</style>
<a href="#">普通链接</a>
<a href="#" class="nav-link">导航链接</a>
东巴文链接伪类顺序::link → :visited → :hover → :active(LVHA顺序)
<!-- ✅ 推荐:描述性文本 -->
<a href="https://db-w.cn/courses/html">
学习HTML教程
</a>
<!-- ❌ 不推荐:无意义文本 -->
<a href="https://db-w.cn/courses/html">
点击这里
</a>
<!-- ❌ 不推荐:使用URL作为文本 -->
<a href="https://db-w.cn/courses/html">
https://db-w.cn/courses/html
</a>
<!-- ✅ 推荐:标识外部链接 -->
<a href="https://example.com" target="_blank" rel="noopener noreferrer">
外部链接 ↗
</a>
<!-- 内部链接 -->
<a href="/courses/html">
HTML教程
</a>
<!-- ✅ 推荐:添加title属性 -->
<a href="https://db-w.cn" title="访问东巴文官网">
东巴文
</a>
<!-- ✅ 推荐:使用aria-label -->
<a href="https://db-w.cn" aria-label="访问东巴文官网">
<img src="logo.png" alt="东巴文">
</a>
<!-- ✅ 推荐:可访问的链接 -->
<a href="#main-content">跳转到主要内容</a>
<!-- ✅ 推荐:焦点样式可见 -->
<style>
a:focus {
outline: 2px solid #0066cc;
outline-offset: 2px;
}
</style>
<!-- ❌ 不推荐:空链接 -->
<a href="#">链接</a>
<a href="javascript:void(0)">链接</a>
<!-- ✅ 推荐:有意义的链接 -->
<a href="/page">链接</a>
<!-- ✅ 推荐:按钮使用button标签 -->
<button type="button">操作</button>
问题1:以下哪个属性用于指定链接在新窗口打开?
A. href
B. target
C. rel
D. title
答案:B
东巴文解释:target属性用于指定链接打开方式,target="_blank"表示在新窗口打开。href指定链接地址,rel定义链接关系,title提供提示文本。
问题2:使用target="_blank"时,应该添加哪个rel属性值?
A. nofollow
B. noopener
C. external
D. author
答案:B
东巴文解释:使用target="_blank"时,应添加rel="noopener"或rel="noopener noreferrer",以防止新页面访问原页面的window.opener,提升安全性。
任务:创建一个包含以下链接类型的文档:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>链接示例 - 东巴文</title>
</head>
<body>
<nav>
<ul>
<li><a href="#section1">第一节</a></li>
<li><a href="#section2">第二节</a></li>
<li><a href="#section3">第三节</a></li>
</ul>
</nav>
<article>
<h1>链接示例</h1>
<section id="section1">
<h2>外部链接</h2>
<p>
访问<a href="https://db-w.cn" target="_blank" rel="noopener noreferrer">
东巴文官网 ↗
</a>
</p>
</section>
<section id="section2">
<h2>联系方式</h2>
<p>
邮箱:<a href="mailto:support@db-w.cn?subject=咨询">support@db-w.cn</a>
</p>
<p>
电话:<a href="tel:+8612345678900">123-4567-8900</a>
</p>
</section>
<section id="section3">
<h2>文件下载</h2>
<p>
<a href="files/tutorial.pdf" download="HTML教程.pdf">
下载HTML教程PDF
</a>
</p>
</section>
</article>
<footer>
<small>版权所有 © 2024 东巴文(db-w.cn)</small>
</footer>
</body>
</html>
</details>
东巴文(db-w.cn) - 让编程学习更有趣、更高效!