Skip to content

Commit

Permalink
[zh-tw]add page called class under global attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
wupojung committed Jan 21, 2025
1 parent 42e05e3 commit 3d29e97
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions files/zh-tw/web/html/global_attributes/class/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
title: class
slug: Web/HTML/Global_attributes/class
page-type: html-attribute
browser-compat: html.global_attributes.class
---

{{HTMLSidebar("Global_attributes")}}

**`class`** [全域屬性](/zh-TW/docs/Web/HTML/Global_attributes) 是一個以 [ASCII 空白字元](/zh-TW/docs/Glossary/Whitespace#in_html) 分隔的元素類別列表。

{{EmbedInteractiveExample("pages/tabbed/attribute-class.html","tabbed-standard")}}

## 描述

類別允許 CSS 和 JavaScript 通過 [類別選擇器](/zh-TW/docs/Web/CSS/Class_selectors) 或 {{domxref("document.getElementsByClassName()")}} 等函數來選擇和訪問特定元素。

雖然規範沒有對類別名稱做出要求,但鼓勵網頁開發人員使用描述元素語義目的的名稱,而不是元素的呈現方式。例如,使用 _attribute_ 來描述屬性而不是使用 _italics_ ,儘管此類別的元素可能會以斜體呈現。即使頁面的呈現方式發生變化,語義名稱仍然保持邏輯性。

### 語法

`class` 屬性是一個以 [ASCII 空白字元](/zh-TW/docs/Glossary/Whitespace#in_html) 分隔的類別值列表。

每個類別值可以包含任何 Unicode 字元(當然,除了 ASCII 空白字元)。 但是,當在 CSS 選擇器中使用時,無論是從 JavaScript 使用 {{domxref("Document.querySelector()")}} 等 API 或是在 CSS 樣式表中,類別屬性值都必須是有效的 [CSS 辨識符](/zh-TW/docs/Web/CSS/ident)。這表示如果類別屬性值不是有效的 CSS 標識符(例如, `my?class``1234`),則在選擇器中使用它之前必須對其進行轉義,可以使用 {{domxref("CSS.escape_static", "CSS.escape()")}} 方法或 [手動](/zh-TW/docs/Web/CSS/ident#escaping_characters) 轉換。

因此,建議開發人員為 class 屬性選擇不需要轉義的有效 CSS 標識符作為值。

## 規範

{{Specifications}}

## 瀏覽器相容性

{{Compat}}

## 參見

- 所有 [全域屬性](/en-US/docs/Web/HTML/Global_attributes).
- {{domxref('element.className')}}
- {{domxref('element.classList')}}
- [CSS 入門](/zh-TW/docs/Learn_web_development/Core/Styling_basics)

0 comments on commit 3d29e97

Please sign in to comment.