href属性的常用值包括绝对url(如https://www.example.com)、相对url(如about.html)、锚点链接(如#sectionid)、电子邮件链接(mailto:email@example.com)和电话链接(tel:+1234567890);2. 控制链接打开方式使用target属性,_self在当前标签页打开,_blank在新标签页打开并建议添加rel="noopener noreferrer"以提高安全性,_parent在父框架打开,_top在整个窗口打开;3. a标签不仅能链接文本,还可包裹图片、图标、div等块级元素,实现图片链接或整个内容卡片可点击,提升交互体验。通过合理使用href、target、rel和title等属性,可创建功能丰富且用户体验良好的超链接。

在HTML中创建超链接,核心就是使用
<a>
<a>
href
创建超链接,最基础的结构是这样的:
<a href="目标URL">链接文本或内容</a>
这里的
href
立即学习“前端免费学习笔记(深入)”;
https://www.example.com/page.html
../images/picture.jpg
about.html
id
#sectionID
mailto:email@example.com
tel:+1234567890
除了
href
<a>
target
_self
_blank
_parent
_top
title
rel
rel="noopener noreferrer"
target="_blank"
示例:
<!-- 链接到外部网站,在新标签页打开,并添加安全属性 --> <p>访问我的<a href="https://www.example.com" target="_blank" rel="noopener noreferrer" title="前往示例网站">个人博客</a>。</p> <!-- 链接到同一网站的另一个页面 --> <p>了解更多关于<a href="about.html">我们</a>。</p> <!-- 链接到当前页面内的某个部分 --> <p><a href="#contact-info">跳到联系方式</a></p> <!-- 电子邮件链接 --> <p>给我发送邮件:<a href="mailto:info@example.com">info@example.com</a></p> <!-- 电话链接 --> <p>拨打我们的电话:<a href="tel:+1234567890">+123 456 7890</a></p> <!-- 链接一张图片 --> <a href="https://unsplash.com/photos/a-person-standing-on-top-of-a-mountain-at-sunset-Q0j-5a9m470"> <img src="sunset.jpg" alt="美丽的日落景色" style="width:200px;"> </a>
href
<a>
首先是完整的网页地址,也就是我们常说的绝对URL。比如你想链接到Google,那就是
<a href="https://www.google.com">Google</a>
其次是相对路径,这在构建网站内部导航时简直是神器。假设你的网站结构是这样的:
/
├── index.html
├── about/
│ └── about.html
└── assets/
└── images/
└── logo.png如果你在
index.html
about.html
<a href="about/about.html">关于我们</a>
about.html
index.html
<a href="../index.html">首页</a>
再来是锚点链接,这在单页应用或者内容很长的页面里特别实用。比如你写了一篇很长的文章,想让读者能快速跳转到某个小节,你就可以在目标小节的某个元素上加上
id
<h2 id="section-intro">引言</h2>
<a href="#section-intro">跳到引言</a>
id="section-intro"
还有一些不那么常见的,但同样有用的
href
href="mailto:your_email@example.com"
href="tel:+1234567890"
<a>
target
最常见的,也是默认的行为,是
target="_self"
然而,当你的链接指向外部网站时,
target="_blank"
target="_blank"
rel="noopener noreferrer"
至于
_parent
_top
<iframe>
<frameset>
_parent
_top
选择正确的
target
<a>
最常见的除了文本,就是图片了。当你把一个
<img>
<a>
<a href="index.html"> <img src="logo.png" alt="网站Logo" style="width:150px;"> </a>
这样,用户点击Logo就能回到首页。这比单纯的文本链接更具视觉引导性。
再进一步,
<a>
<div>
<p>
<h1>
<a>
<a>
div
<a href="product-detail.html" style="display: block; text-decoration: none; color: inherit;">
<div class="product-card">
<h3>精美手工项链</h3>
<img src="necklace.jpg" alt="手工项链" style="width:100%;">
<p>这款项链采用XXXX材质,独家设计。</p>
<span>点击查看详情</span>
</div>
</a>注意这里我加了
display: block;
此外,你也可以链接图标(通常是字体图标或SVG图标),或者将多个行内元素(如
<span>
<em>
<a>
<a>
以上就是如何在HTML中创建超链接?a标签怎么用?的详细内容,更多请关注php中文网其它相关文章!
HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号