此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

Element.className

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2015年7月⁩.

className 获取或设置指定元素的 class 属性的值。

一个表示当前元素的 class 或以空格分隔的多个 class 的字符串变量。

示例

js
let elm = document.getElementById("div1");

if (elm.className == "fixed") {
  // 跳过 class 属性为特定值的元素
  goNextElement();
}

注释

备注: 使用名称className而不是class作为属性名,是因为"class" 在 JavaScript 中是个保留字。

规范

Specification
DOM
# ref-for-dom-element-classname①

浏览器兼容性

参见