概述

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

语法

let cName = elementNodeReference.className;

elementNodeReference.className = cName;
  • cName是一个字符串变量,表示当前元素的class属性的值,可以是由空格分隔的多个class属性值.

示例

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

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

注释

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

规范

Specification Status Comment
DOM
element.className
Living Standard  
DOM4
element.className
Obsolete  
Document Object Model (DOM) Level 2 HTML Specification
element.className
Obsolete Initial definition

浏览器兼容性

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!

Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)
Feature Android Chrome for Android Edge Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (Yes) (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)

相关链接

文档标签和贡献者

标签: 
最后编辑者: glbjiiii,