HTML DOM className 属性
JavaScript基础 2022-06-08 11:28:3812小码哥的IT人生shichen
HTML DOM className 属性
定义和用法
className 属性设置或返回元素的 class 属性。
语法
HTMLElementObject.className=classname
浏览器支持
IE | Firefox | Chrome | Safari | Opera |
---|---|---|---|---|
所有主流浏览器都支持 className 属性。
实例
返回 body 元素的 class 属性:
<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>
输出:
Body CSS class: mystyle An alternate way: mystyle
完整实例:
<!DOCTYPE html> <html> <body id="myid" class="mystyle"> <script> var x=document.getElementsByTagName('body')[0]; document.write("Body CSS 类:" + x.className); document.write("<br>"); document.write("另一个替代方法:"); document.write(document.getElementById('myid').className); </script> </body> </html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html