前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >小白前端入门笔记(10),怎么设置网站内部的超链接?

小白前端入门笔记(10),怎么设置网站内部的超链接?

作者头像
TechFlow-承志
发布2021-02-08 09:45:50
1.3K0
发布2021-02-08 09:45:50
举报
文章被收录于专栏:TechFlow

大家好,欢迎来到freecodecamp HTML专题第10篇。

今天的挑战依然关于a标签。

背景知识

a元素除了可以指向外部的一个网站之外,同样也可以用来跳转到网站内部的某一个区域。这一点大家都应该体会过,当我们看一篇长文博客,我们经常通过点击小标题的方式超链接到文章当中具体的位置。

想要创建一个内部的链接,同样需要使用href这个属性,我们将它设置成'#'加上具体的id的形式来实现。这里的id是HTML当中的一个通用属性,任何标签都可以加上idid作为一个标识符,可以作为CSS样式的设置目标,也可以作为a标签的跳转目标。需要注意的是对于每一个元素赋予的id都应该是唯一的。

下面我们展示一个通过id完成内部跳转的例子:

代码语言:javascript
复制
<a href="#contacts-header">Contacts</a>
...
<h2 id="contacts-header">Contacts</h2>

我们给h2标签赋予了一个id属性,当我们点击a标签的时候,它就会跳转到这个id标记的区域。

题意

通过修改href属性的值将网站当中外部的标签修改成内部的标签'#footer',并且把对应的文本从"cat photos"改成"Jump to Bottom"。

移除a标签中target=_blank属性,因为它会使得用户在点击链接的时候打开一个新的页面。

之后在<footer>标签当中加上一个为"footer"的id属性。

要求

  1. 网站当中只能有一个a标签
  2. 网站当中只能有一个footer标签
  3. a标签需要有一个指向"#footer"的href属性
  4. a标签不能有target属性
  5. a标签的文本应该为"Jump to Bottom"
  6. footer标签需要有一个为"footer"的id属性

编辑器

代码语言:javascript
复制
<h2>CatPhotoApp</h2>
<main>

  <a href="https://freecatphotoapp.com" target="_blank">cat photos</a>

  <img src="https://bit.ly/fcc-relaxing-cat" alt="A cute orange cat lying on its back.">

  <p>Kitty ipsum dolor sit amet, shed everywhere shed everywhere stretching attack your ankles chase the red dot, hairball run catnip eat the grass sniff. Purr jump eat the grass rip the couch scratched sunbathe, shed everywhere rip the couch sleep in the sink fluffy fur catnip scratched. Kitty ipsum dolor sit amet, shed everywhere shed everywhere stretching attack your ankles chase the red dot, hairball run catnip eat the grass sniff.</p>
  <p>Purr jump eat the grass rip the couch scratched sunbathe, shed everywhere rip the couch sleep in the sink fluffy fur catnip scratched. Kitty ipsum dolor sit amet, shed everywhere shed everywhere stretching attack your ankles chase the red dot, hairball run catnip eat the grass sniff. Purr jump eat the grass rip the couch scratched sunbathe, shed everywhere rip the couch sleep in the sink fluffy fur catnip scratched.</p>
  <p>Meowwww loved it, hated it, loved it, hated it yet spill litter box, scratch at owner, destroy all furniture, especially couch or lay on arms while you're using the keyboard. Missing until dinner time toy mouse squeak roll over. With tail in the air lounge in doorway. Man running from cops stops to pet cats, goes to jail.</p>
  <p>Intently stare at the same spot poop in the plant pot but kitten is playing with dead mouse. Get video posted to internet for chasing red dot leave fur on owners clothes meow to be let out and mesmerizing birds leave fur on owners clothes or favor packaging over toy so purr for no reason. Meow to be let out play time intently sniff hand run outside as soon as door open yet destroy couch.</p>

</main>

<footer>Copyright Cat Photo App</footer>

解答

今天的挑战很简单,其实就是按照设置内部跳转的方式来修改一下html代码。主要分为三个步骤,一个步骤是在footer标签当中加上我们跳转的目标,也就是id属性。接着在a标签当中修改跳转的目标,从外部的链接改成内部的id。最后我们改一下a标签的文本信息即可。

代码语言:javascript
复制
<h2>CatPhotoApp</h2>
<main>

  <a href="#footer">Jump to Bottom</a>

  <img src="https://bit.ly/fcc-relaxing-cat" alt="A cute orange cat lying on its back.">

  <p>Kitty ipsum dolor sit amet, shed everywhere shed everywhere stretching attack your ankles chase the red dot, hairball run catnip eat the grass sniff. Purr jump eat the grass rip the couch scratched sunbathe, shed everywhere rip the couch sleep in the sink fluffy fur catnip scratched. Kitty ipsum dolor sit amet, shed everywhere shed everywhere stretching attack your ankles chase the red dot, hairball run catnip eat the grass sniff.</p>
  <p>Purr jump eat the grass rip the couch scratched sunbathe, shed everywhere rip the couch sleep in the sink fluffy fur catnip scratched. Kitty ipsum dolor sit amet, shed everywhere shed everywhere stretching attack your ankles chase the red dot, hairball run catnip eat the grass sniff. Purr jump eat the grass rip the couch scratched sunbathe, shed everywhere rip the couch sleep in the sink fluffy fur catnip scratched.</p>
  <p>Meowwww loved it, hated it, loved it, hated it yet spill litter box, scratch at owner, destroy all furniture, especially couch or lay on arms while you're using the keyboard. Missing until dinner time toy mouse squeak roll over. With tail in the air lounge in doorway. Man running from cops stops to pet cats, goes to jail.</p>
  <p>Intently stare at the same spot poop in the plant pot but kitten is playing with dead mouse. Get video posted to internet for chasing red dot leave fur on owners clothes meow to be let out and mesmerizing birds leave fur on owners clothes or favor packaging over toy so purr for no reason. Meow to be let out play time intently sniff hand run outside as soon as door open yet destroy couch.</p>

</main>

<footer id="footer">Copyright Cat Photo App</footer>
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-01-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Coder梁 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 背景知识
  • 题意
    • 要求
      • 编辑器
      • 解答
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档