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

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

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

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

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

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

<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属性。

要求

  • 网站当中只能有一个a标签
  • 网站当中只能有一个footer标签
  • a标签需要有一个指向"#footer"的href属性
  • a标签不能有target属性
  • a标签的文本应该为"Jump to Bottom"
  • footer标签需要有一个为"footer"的id属性
    编辑器
<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标签的文本信息即可。

<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>

想要亲自动手尝试一下的同学不要忘了点击文末的阅读原文进行跳转哦~

文章就到这里,给个三连再走吧~