MDN’s new design is in Beta! A sneak peek: https://blog.mozilla.org/opendesign/mdns-new-design-beta/

概述

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
Recommendation  
Document Object Model (DOM) Level 2 HTML Specification
element.className
Recommendation Initial definition

浏览器兼容性

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)

相关链接

文档标签和贡献者

标签: 
 此页面的贡献者: xgqfrms-GitHub, teoli, khalid32, AlexChao, ziyunfei, dextra, Dewang
 最后编辑者: xgqfrms-GitHub,