2018-10-10 18:03:03 -04:00
---
id: bad87fee1348bd9aede08817
challengeType: 0
2019-12-26 20:05:59 +08:00
videoUrl: 'https://scrimba.com/p/pVMPUv/cb6k8Cb'
forumTopicId: 18244
2020-10-01 17:54:21 +02:00
title: 将 a 嵌套在段落中
2018-10-10 18:03:03 -04:00
---
## Description
2019-12-26 20:05:59 +08:00
< section id = 'description' >
你可以在其他文本元素内嵌套链接。
```html
< p >
Here's a < a target = "_blank" href = "http://freecodecamp.org" > link to freecodecamp.org< / a > for you to follow.
< / p >
```
让我们来分解这个例子:
通常,文本是被包裹在< code > p</ code > 段落内:< br >< code > & #60 ; p& #62 ; Here's a ... for you to follow. & #60 ; /p& #62 ; </ code >
接下来是< code > anchor</ code > < code > a</ code > < code > & #60 ; a& #62 ; </ code > (需要结束标记 < code > & #60 ; /a& #62 ; </ code > ) :< br > < code > & #60 ; a& #62 ; ... & #60 ; /a& #62 ; </ code >
< code > target< / code > 是 < code > a< / code > 的一个属性,用来指定链接的打开方式。属性值 < code > "_blank"< / code > 的意思是链接会在新标签页打开。
2020-08-13 17:35:04 +02:00
< code > href</ code > 是 < code > a</ code > 的另一个属性:用来指定链接的 URL: < br > `<a href="https://freecodecamp.org"> ... </a>`
2019-12-26 20:05:59 +08:00
< code > a</ code > 元素内的文本:< strong > "link to freecodecamp.org"</ strong > ,会显示为一个可以点击的链接:< br > < code > & #60 ; a href=" ... "& #62 ; link to freecodecamp.org& #60 ; /a& #62 ; </ code >
例子的最后输出将会是这样:< br > < p > Here's a < a target = "_blank" href = "http://freecodecamp.one" > link to freecodecamp.org< / a > for you to follow.< / p >
< / section >
2018-10-10 18:03:03 -04:00
## Instructions
2019-12-26 20:05:59 +08:00
< section id = 'instructions' >
用一个段落(< code > p< / code > )标签来包裹< code > main< / code > 元素里的< code > a< / code > 节点。新段落的文本为: “View more cat photos”, 其中 "cat photos" 是一个链接,其余是纯文本。
< / section >
2018-10-10 18:03:03 -04:00
## Tests
< section id = 'tests' >
```yml
tests:
2020-07-15 02:55:06 -07:00
- text: '你需要一个指向 "https://freecatphotoapp.com" 的 < code > a</ code > 。'
testString: assert(($("a[href=\"https://freecatphotoapp.com\"]").length > 0 || $("a[href=\"http://www.freecatphotoapp.com\"]").length > 0));
2019-12-26 20:05:59 +08:00
- text: '< code > a</ code > 的文本应为: cat photos。'
testString: assert($("a").text().match(/cat\sphotos/gi));
- text: '在 < code > a</ code > 的外部创建一个新段落,这样页面就有 3 个段落了。'
testString: assert($("p") & & $("p").length > 2);
- text: '< code > a</ code > 应嵌套在新段落内。'
2020-07-15 02:55:06 -07:00
testString: assert(($("a[href=\"https://freecatphotoapp.com\"]").parent().is("p") || $("a[href=\"http://www.freecatphotoapp.com\"]").parent().is("p")));
2019-12-26 20:05:59 +08:00
- text: '段落应该包含文本 View more( 记得 more 后面有一个空格)。'
2020-07-15 02:55:06 -07:00
testString: assert(($("a[href=\"https://freecatphotoapp.com\"]").parent().text().match(/View\smore\s/gi) || $("a[href=\"http://www.freecatphotoapp.com\"]").parent().text().match(/View\smore\s/gi)));
2019-12-26 20:05:59 +08:00
- text: '< code > a</ code > 不应该包含文本 View more。'
testString: assert(!$("a").text().match(/View\smore/gi));
- text: '确保每个段落有结束标记。'
testString: assert(code.match(/< \/p > /g) && code.match(/< p / g ) && code . match (/< \/p > /g).length === code.match(/< p / g ). length );
- text: '确保每个段落有结束标记。'
testString: 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
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'html-seed' >
```html
< h2 > CatPhotoApp< / h2 >
< main >
2019-12-26 20:05:59 +08:00
2020-07-15 02:55:06 -07:00
< a href = "https://freecatphotoapp.com" target = "_blank" > cat photos< / a >
2019-12-26 20:05:59 +08:00
< img src = "https://bit.ly/fcc-relaxing-cat" alt = "一只仰卧着的萌猫" >
< p > 在大家心目中,猫是慵懒和可爱的化身,它可以睡饱了再起来吃饭,可以逗趣小耗子,可以卖得了萌,使得了坏,这样百变的小怪兽就集结在一只宠物上,怎能不惹人怜爱。< / p >
< p > 养猫有的时候,就是介于爱与恨之间,当你钦羡别人萌宠这么可爱的时候,你一定没有想过,猫咪会到处掉毛,甚至会屯老鼠,啃鞋子,用爪子爬门,你不理它,它就挠你,你要对它发脾气,它会比你更来劲。所以,猫咪慎入,没有一定的准备,切勿随便去侍养动物。它们一旦认定你了,你就是它们的主人,如果你抛弃它们,它们必定心中重创。< / p >
2018-10-10 18:03:03 -04:00
< / main >
```
< / div >
< / section >
## Solution
< section id = 'solution' >
2020-07-15 02:55:06 -07:00
2018-10-10 18:03:03 -04:00
< / section >
2020-07-15 02:55:06 -07:00