---
id: bad87fee1348bd9aed308826
title: 使用 jQuery 選擇元素的父元素
challengeType: 6
forumTopicId: 18321
dashedName: target-the-parent-of-an-element-using-jquery
---
# --description--
每個 HTML 標籤都默認 `inherits`(繼承)其 `parent`(父標籤)的 CSS 屬性。
例如,`h3` 標籤 `jQuery Playground` 的父標籤是 `
`,而這個標籤的父標籤是 `body`。
jQuery 有一個 `parent()` 方法,可以訪問被選取標籤的父標籤。
下面的代碼展示了使用 `parent()` 方法把 `left-well` 標籤的父標籤背景色設置成藍色(blue):
```js
$("#left-well").parent().css("background-color", "blue")
```
把 `#target1` 元素的父元素背景色設置成紅色(red)。
# --hints--
`left-well` 元素應該有紅色的背景。
```js
assert(
$('#left-well').css('background-color') === 'red' ||
$('#left-well').css('background-color') === 'rgb(255, 0, 0)' ||
$('#left-well').css('background-color').toLowerCase() === '#ff0000' ||
$('#left-well').css('background-color').toLowerCase() === '#f00'
);
```
應該用 `.parent()` 函數修改該元素。
```js
assert(code.match(/\.parent\s*\(\s*\)\s*\.css/g));
```
應該在 `#target1` 元素上調用 `.parent()` 方法。
```js
assert(
code.match(/\$\s*?\(\s*?(?:'|")\s*?#target1\s*?(?:'|")\s*?\)\s*?\.parent/gi)
);
```
應該僅用 jQuery 給元素添加 class。
```js
assert(code.match(/
/g));
```
# --seed--
## --seed-contents--
```html
jQuery Playground
#left-well
#right-well
```
# --solutions--
```html
jQuery Playground
#left-well
#right-well
```