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
|
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`(Anchor,简写 a)来实现网页间的跳转。
|
|
|
|
|
|
|
|
|
|
`a` 需要一个`href`属性指向目的地,它还需要有 `a` 文本,例如:
|
|
|
|
|
|
|
|
|
|
`<a href="https://freecodecamp.org">传送至</a>`
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
然后你的浏览器会显示一个可以点击的文本,点击该文本就会跳转到`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
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
创建一个 `a`,它的`href`属性为`https://freecatphotoapp.com` ,它的文本为`cat photos`。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --hints--
|
|
|
|
|
|
|
|
|
|
`a`元素的 `a` 文本应为:`cat photos`。
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert(/cat photos/gi.test($('a').text()));
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
`a`元素的`href`属性应为:"`http://freecatphotoapp<wbr>.com`"。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert(/http:\/\/(www\.)?freecatphotoapp\.com/gi.test($('a').attr('href')));
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
确保`a`元素有结束标记。
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert(
|
|
|
|
|
code.match(/<\/a>/g) &&
|
|
|
|
|
code.match(/<\/a>/g).length === code.match(/<a/g).length
|
|
|
|
|
);
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --solutions--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|