# 4.4 更新節點

## 屬性

### .setAttribute() 或 點語法(dot syntax)

任何屬性都可以透過 `.setAttribute()` 來更新；或者有些有些可以透過 「點語法(dot syntax)」。

需帶入兩個參數：

* 第一個：欲更新的屬性名稱。
* 第二個：欲設定的值。

```javascript
var para = document.getElementById("para1");
para.setAttribute("style", "color: red;");
```

假設有個按鈕，點擊後，狀態變成 **`disabled`**：

```javascript
var btn = document.getElementById("btn");
btn.addEventListener("click", function(){
  
  this.disabled = true;
});
```

例：

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

## 內容

### .innerHTML

更新元素裡的全部內容，原內容會整個被清空。\
然後更新的內容中，如果有 HTML 相關原始碼的話，會當做 HTML 來看待。

例：

```javascript
var para = document.getElementById("para1");
para.innerHTML = "<strong>新的文字</strong>";
```

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

### .innerText

更新元素裡的全部內容，原內容會整個被清空。\
然後更新的內容中，如果有 HTML 相關原始碼的話，會當做 **純文字** 來看待。

例：

```javascript
var para = document.getElementById("para1");
para.innerText = "<strong>新的文字</strong>";
```

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


---

# 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-web/4.-dom-wen-jian-wu-jian-mo-xing/4.4-geng-xin-jie-dian.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.
