# 4.7 操控 class 屬性

## .classList.add()

讓某元素加上指定的 class。

## .classList.remove()

讓某元素移除指定的 class。

## .classList.contains()

判斷某元素是否存在某個 class。

## .classList.toggle()

某元素若存在某個 class，就移除；反之，就加上。

## 範例

{% embed url="<https://codepen.io/carlos411/pen/QWNdEez>" %}

## .style 的寫法

```javascript
var the_block = document.getElementById("the_block");
the_block.style.opacity = 0.2;
the_block.style.backgroundColor = "blue";
```

範例：

{% embed url="<https://codepen.io/carlos411/pen/RwaQmmr>" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.webmix.cc/javascript-class/4.-dom-wen-jian-wu-jian-mo-xing/4.7-cao-kong-class-shu-xing.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
