2018-10-10 18:03:03 -04:00
---
id: bad87fee1348bd9aedf08820
2020-12-16 00:37:30 -07:00
title: 给图片添加链接
2018-10-10 18:03:03 -04:00
challengeType: 0
2019-12-26 20:05:59 +08:00
videoUrl: 'https://scrimba.com/p/pVMPUv/cRdBnUr'
forumTopicId: 18327
2021-01-13 03:31:00 +01:00
dashedName: turn-an-image-into-a-link
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
2020-12-16 00:37:30 -07:00
你可以通过把元素嵌套进 `a` 里使其变成一个链接。
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
如果我们要把图片嵌套进 `a` 元素, 可以这样写:
2018-10-10 18:03:03 -04:00
2021-03-21 10:58:20 -06:00
```html
2021-09-22 08:34:59 -07:00
< a href = "#" > < img src = "https://cdn.freecodecamp.org/curriculum/cat-photo-app/relaxing-cat.jpg" alt = "Three kittens running towards the camera." > < / a >
2021-03-21 10:58:20 -06:00
```
2020-12-16 00:37:30 -07:00
2021-02-06 04:42:36 +00:00
如果把 `a` 的 `href` 属性值设置为 `#` ,创建的是一个死链接(不跳转到其他画面)。
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-02-06 04:42:36 +00:00
请把现存的图片嵌套进 `a` ( *锚点* )元素中。
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
完成后,请你把鼠标光标悬停在你的图像上, 鼠标光标将变成点击光标。 于是图片就变成了链接。
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
# --hints--
2021-02-06 04:42:36 +00:00
应将 `img` 嵌套进 `a` 元素中。
2020-12-16 00:37:30 -07:00
```js
assert($('a').children('img').length > 0);
2018-10-10 18:03:03 -04:00
```
2021-01-08 11:20:48 -08:00
`a` 的 `href` 属性值应为 `#` 。
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(new RegExp('#').test($('a').children('img').parent().attr('href')));
```
2018-10-10 18:03:03 -04:00
2021-03-21 10:58:20 -06:00
每个 `a` 元素都应该有一个结束标签。
2020-12-16 00:37:30 -07:00
```js
assert(
code.match(/< \/a > /g) &&
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 >
< p > Click here to view more < a href = "#" > cat photos< / a > .< / p >
2021-09-22 08:34:59 -07:00
< img src = "https://cdn.freecodecamp.org/curriculum/cat-photo-app/relaxing-cat.jpg" alt = "A cute orange cat lying on its back." >
2021-01-13 03:31:00 +01:00
< 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 >
< p > Click here to view more < a href = "#" > cat photos< / a > .< / p >
2021-02-06 04:42:36 +00:00
2021-09-22 08:34:59 -07:00
< a href = "#" > < img src = "https://cdn.freecodecamp.org/curriculum/cat-photo-app/relaxing-cat.jpg" alt = "A cute orange cat lying on its back." > < / a >
2021-02-06 04:42:36 +00:00
2021-01-13 03:31:00 +01:00
< 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 >
```