--- id: bad87eee1348bd9aede07836 title: 設置元素的 id challengeType: 0 videoUrl: 'https://scrimba.com/c/cN6MEc2' forumTopicId: 18279 dashedName: set-the-id-of-an-element --- # --description-- 除了 class 屬性,每一個 HTML 元素都有一個 `id` 屬性。 使用 `id` 有幾個好處:你可以通過 `id` 選擇器來改變單個元素的樣式。在稍後的課程中,你還會了解到如何在 JavaScript 裏面用它來選擇和操作元素。 根據規範,`id` 屬性應是唯一的。 儘管瀏覽器並非必須執行這條規範,但這是廣泛認可的最佳實踐。 因此,請不要給多個元素設置相同的 `id`。 設置 `h2` 元素的 id 爲 `cat-photo-app` 的代碼如下: ```html

``` # --instructions-- 設置`form`元素的 id 爲`cat-photo-form`。 # --hints-- `form` 元素的 id 應爲 `cat-photo-form`。 ```js assert($('form').attr('id') === 'cat-photo-form'); ``` # --seed-- ## --seed-contents-- ```html

CatPhotoApp

Click here to view more cat photos.

A cute orange cat lying on its back.

Things cats love:

Top 3 things cats hate:

  1. flea treatment
  2. thunder
  3. other cats


``` # --solutions-- ```html

CatPhotoApp

Click here to view more cat photos.

A cute orange cat lying on its back.

Things cats love:

Top 3 things cats hate:

  1. flea treatment
  2. thunder
  3. other cats


```