2018-10-10 18:03:03 -04:00
---
id: bad87fee1348bd9aedf08816
2020-12-16 00:37:30 -07:00
title: 用 a 实现网页间的跳转
2018-10-10 18:03:03 -04:00
challengeType: 0
2019-12-26 20:05:59 +08:00
videoUrl: 'https://scrimba.com/p/pVMPUv/c8EkncB'
forumTopicId: 18226
2021-01-13 03:31:00 +01:00
dashedName: link-to-external-pages-with-anchor-elements
2018-10-10 18:03:03 -04:00
---
2020-12-16 00:37:30 -07:00
# --description--
2018-10-10 18:03:03 -04:00
2021-01-08 11:20:48 -08:00
你可以用 `a` ( Anchor, 简写为 a) 来实现网页间的跳转。
2020-12-16 00:37:30 -07:00
2021-01-20 18:58:05 -08:00
`a` 需要一个 `href` 属性指向跳转的目的地。 同时,它还应有内容。 例如:
2020-12-16 00:37:30 -07:00
2021-01-08 11:20:48 -08:00
`<a href="https://freecodecamp.org">链接到 freeCodeCamp</a>`
2018-10-10 18:03:03 -04:00
2021-01-20 18:58:05 -08:00
在浏览器中,以上的标签会将文字 ** "链接到 freeCodeCamp"** 展示成一个可点击的超链接。 点击该文本就会跳转到 ** `https://freecodecamp.org` **。
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
# --instructions--
2018-10-10 18:03:03 -04:00
2021-01-20 18:58:05 -08:00
创建一个内容文本为 “cat photos” 的 `a` 元素,并将其 `href` 属性值设置为 `https://freecatphotoapp.com` 。
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
# --hints--
2021-01-20 18:58:05 -08:00
`a` 元素的锚文本应为 “cat photos”。
2020-12-16 00:37:30 -07:00
```js
assert(/cat photos/gi.test($('a').text()));
2018-10-10 18:03:03 -04:00
```
2021-01-08 11:20:48 -08:00
你的 `a` 元素应链接到 `https://freecatphotoapp.com` 。
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
2021-01-20 18:58:05 -08:00
assert(/https:\/\/(www\.)?freecatphotoapp\.com/gi.test($('a').attr('href')));
2020-12-16 00:37:30 -07:00
```
2018-10-10 18:03:03 -04:00
2021-01-08 11:20:48 -08:00
确保 `a` 元素有结束标签。
2020-12-16 00:37:30 -07:00
```js
assert(
code.match(/< \/a > /g) &&
code.match(/< \/a > /g).length === code.match(/< a / g ). length
);
```
2018-10-10 18:03:03 -04:00
2021-01-13 03:31:00 +01:00
# --seed--
## --seed-contents--
```html
< h2 > CatPhotoApp< / h2 >
< main >
< 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.< / 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.< / p >
< / main >
```
2020-12-16 00:37:30 -07:00
# --solutions--
2018-10-10 18:03:03 -04:00
2021-01-13 03:31:00 +01:00
```html
< h2 > CatPhotoApp< / h2 >
< main >
2021-01-20 18:58:05 -08:00
2021-01-13 03:31:00 +01:00
< img src = "https://bit.ly/fcc-relaxing-cat" alt = "A cute orange cat lying on its back." >
2021-01-20 18:58:05 -08:00
2021-01-13 03:31:00 +01:00
< a href = "https://freecatphotoapp.com" > cat photos< / a >
< 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.< / 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.< / p >
< / main >
```