2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
id: bad87fee1348bd9aedf08816
|
2020-12-16 07:37:30 +00:00
|
|
|
|
title: 用 a 实现网页间的跳转
|
2018-10-10 22:03:03 +00:00
|
|
|
|
challengeType: 0
|
2019-12-26 12:05:59 +00:00
|
|
|
|
videoUrl: 'https://scrimba.com/p/pVMPUv/c8EkncB'
|
|
|
|
|
forumTopicId: 18226
|
2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --description--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
|
你可以用 `a`(Anchor,简写为 a)来实现网页间的跳转。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
|
`a` 需要一个 `href` 属性指向跳转的目的地,同时,它还应有内容,例如:
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
|
`<a href="https://freecodecamp.org">链接到 freeCodeCamp</a>`
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
|
在浏览器中,以上的标签会将文字 **"链接到 freeCodeCamp"** 展示成一个可点击的超链接。点击该文本就会跳转到 `https://freecodecamp.org`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --instructions--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
|
创建一个内容文本为 `cat photos` 的 `a` 元素,并将其 `href` 属性值设置为 `https://freecatphotoapp.com`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --hints--
|
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
|
`a` 元素的内容文本应为:`cat photos`。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert(/cat photos/gi.test($('a').text()));
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
|
你的 `a` 元素应链接到 `https://freecatphotoapp.com`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(/http:\/\/(www\.)?freecatphotoapp\.com/gi.test($('a').attr('href')));
|
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
|
确保 `a` 元素有结束标签。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert(
|
|
|
|
|
code.match(/<\/a>/g) &&
|
|
|
|
|
code.match(/<\/a>/g).length === code.match(/<a/g).length
|
|
|
|
|
);
|
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --solutions--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|