HTML DOM classNameProperty

元素对象参考手册Element Object


Definition and Usage

The className property sets or returns the class attribute of an element.

Syntax

Get the attribute value:

HTMLElementObject.className

Set the attribute value:

HTMLElementObject.className=classname


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the className property


Example

Example

Return the class attribute of the body element:

<html>
<body id="myid" class="mystyle">

<script>
var x=document.getElementsByTagName('body')[0];
document.write("Body CSS class: " + x.className);
document.write("<br>");
document.write("An alternate way: ");
document.write(document.getElementById('myid').className);
</script>

</body>
</html>

Output result:

Body CSS class: mystyle
An alternate way: mystyle

Try it yourself »


元素对象参考手册Element Object

Other Extensions