2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
id: bad88fee1348bd9aedf08816
|
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/cyrDRUL'
|
|
|
|
forumTopicId: 301098
|
2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --description--
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
`a` 元素还可以用来实现页面内不同区域的跳转,只需要把 `a` 元素的 `href` 值设置为井号 `#` 加欲跳转区域所对应的 `id` 属性值即可。`id` 是描述网页元素的一个属性,它的值在整个页面中唯一。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
下面是用来创建内部 `a` 的例子:
|
2019-12-26 12:05:59 +00:00
|
|
|
|
|
|
|
```html
|
|
|
|
<a href="#contacts-header">Contacts</a>
|
|
|
|
...
|
|
|
|
<h2 id="contacts-header">Contacts</h2>
|
|
|
|
```
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
当用户点击了 `Contacts` 链接,页面就会跳转到网页的 **Contacts** 区域。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
# --instructions--
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
请修改 `href` 属性值为 `#footer` 来将外部链接更改为内部链接,同时修改文本 `cat photos` 为 `Jump to Bottom`。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
移除 `target="\_blank"` 属性,这个属性的存在会让链接在新标签页中打开。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
然后添加一个 `<footer>` 元素,并将它的 `id` 属性值设置为 `footer`。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
# --hints--
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
页面中应只存在一个 `a` 元素。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert($('a').length == 1);
|
|
|
|
```
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
页面中应只存在一个 `footer` 元素。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert($('footer').length == 1);
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
`a` 的 `href` 属性值应为 `#footer`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert($('a').eq(0).attr('href') == '#footer');
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
`a` 不应有 `target` 属性。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert(
|
|
|
|
typeof $('a').eq(0).attr('target') == typeof undefined ||
|
|
|
|
$('a').eq(0).attr('target') == true
|
|
|
|
);
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
`a` 的内容文本应为 `Jump to Bottom`。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(
|
|
|
|
$('a')
|
|
|
|
.eq(0)
|
|
|
|
.text()
|
|
|
|
.match(/Jump to Bottom/gi)
|
|
|
|
);
|
|
|
|
```
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
`footer` 元素的 `id` 属性值应为 `footer`。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert($('footer').eq(0).attr('id') == 'footer');
|
|
|
|
```
|
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
|
|
|
|