2018-10-10 18:03:03 -04:00
---
id: bad87eee1348bd9aede07836
2020-12-16 00:37:30 -07:00
title: 设置元素的 id
2018-10-10 18:03:03 -04:00
challengeType: 0
2019-12-13 13:47:57 +08:00
videoUrl: 'https://scrimba.com/c/cN6MEc2'
forumTopicId: 18279
2021-01-13 03:31:00 +01:00
dashedName: set-the-id-of-an-element
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
2021-01-08 11:20:48 -08:00
除了 class 属性,每一个 HTML 元素都有一个 `id` 属性。
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
使用 `id` 有几个好处:你可以通过 `id` 选择器来改变单个元素的样式。在稍后的课程中,你还会了解到如何在 JavaScript 里面用它来选择和操作元素。
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
根据规范,`id` 属性应是唯一的。 尽管浏览器并非必须执行这条规范,但这是广泛认可的最佳实践。 因此,请不要给多个元素设置相同的 `id` 。
2018-10-10 18:03:03 -04:00
2021-01-08 11:20:48 -08:00
设置 `h2` 元素的 id 为 `cat-photo-app` 的代码如下:
2018-10-10 18:03:03 -04:00
2021-03-21 10:58:20 -06:00
```html
< h2 id = "cat-photo-app" >
```
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-03-21 10:58:20 -06:00
设置`form` 元素的 id 为`cat-photo-form` 。
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
# --hints--
2018-10-10 18:03:03 -04:00
2021-01-08 11:20:48 -08:00
`form` 元素的 id 应为 `cat-photo-form` 。
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert($('form').attr('id') === 'cat-photo-form');
2018-10-10 18:03:03 -04:00
```
2021-01-13 03:31:00 +01:00
# --seed--
## --seed-contents--
```html
< link href = "https://fonts.googleapis.com/css?family=Lobster" rel = "stylesheet" type = "text/css" >
< style >
.red-text {
color: red;
}
h2 {
font-family: Lobster, monospace;
}
p {
font-size: 16px;
font-family: monospace;
}
.thick-green-border {
border-color: green;
border-width: 10px;
border-style: solid;
border-radius: 50%;
}
.smaller-image {
width: 100px;
}
.silver-background {
background-color: silver;
}
< / style >
< h2 class = "red-text" > CatPhotoApp< / h2 >
< main >
< p class = "red-text" > Click here to view more < a href = "#" > cat photos< / a > .< / p >
2021-09-22 08:34:59 -07:00
< a href = "#" > < img class = "smaller-image thick-green-border" src = "https://cdn.freecodecamp.org/curriculum/cat-photo-app/relaxing-cat.jpg" alt = "A cute orange cat lying on its back." > < / a >
2021-01-13 03:31:00 +01:00
< div class = "silver-background" >
< p > Things cats love:< / p >
< ul >
< li > cat nip< / li >
< li > laser pointers< / li >
< li > lasagna< / li >
< / ul >
< p > Top 3 things cats hate:< / p >
< ol >
< li > flea treatment< / li >
< li > thunder< / li >
< li > other cats< / li >
< / ol >
< / div >
< form action = "https://freecatphotoapp.com/submit-cat-photo" >
< label > < input type = "radio" name = "indoor-outdoor" checked > Indoor< / label >
< label > < input type = "radio" name = "indoor-outdoor" > Outdoor< / label > < br >
< label > < input type = "checkbox" name = "personality" checked > Loving< / label >
< label > < input type = "checkbox" name = "personality" > Lazy< / label >
< label > < input type = "checkbox" name = "personality" > Energetic< / label > < br >
< input type = "text" placeholder = "cat photo URL" required >
< button type = "submit" > Submit< / button >
< / form >
< / main >
```
2020-12-16 00:37:30 -07:00
# --solutions--
2020-02-18 01:40:55 +09:00
2021-01-13 03:31:00 +01:00
```html
< link href = "https://fonts.googleapis.com/css?family=Lobster" rel = "stylesheet" type = "text/css" >
< style >
.red-text {
color: red;
}
h2 {
font-family: Lobster, monospace;
}
p {
font-size: 16px;
font-family: monospace;
}
.thick-green-border {
border-color: green;
border-width: 10px;
border-style: solid;
border-radius: 50%;
}
.smaller-image {
width: 100px;
}
.silver-background {
background-color: silver;
}
< / style >
< h2 class = "red-text" > CatPhotoApp< / h2 >
< main >
< p class = "red-text" > 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 class = "smaller-image thick-green-border" 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
< div class = "silver-background" >
< p > Things cats love:< / p >
< ul >
< li > cat nip< / li >
< li > laser pointers< / li >
< li > lasagna< / li >
< / ul >
< p > Top 3 things cats hate:< / p >
< ol >
< li > flea treatment< / li >
< li > thunder< / li >
< li > other cats< / li >
< / ol >
< / div >
2021-02-06 04:42:36 +00:00
2021-01-13 03:31:00 +01:00
< form action = "https://freecatphotoapp.com/submit-cat-photo" id = "cat-photo-form" >
< label > < input type = "radio" name = "indoor-outdoor" checked > Indoor< / label >
< label > < input type = "radio" name = "indoor-outdoor" > Outdoor< / label > < br >
< label > < input type = "checkbox" name = "personality" checked > Loving< / label >
< label > < input type = "checkbox" name = "personality" > Lazy< / label >
< label > < input type = "checkbox" name = "personality" > Energetic< / label > < br >
< input type = "text" placeholder = "cat photo URL" required >
< button type = "submit" > Submit< / button >
< / form >
< / main >
```