搜索文档
首页
HTML/CSS
JavaScript
服务端开发
Java教程
移动端
数据库
当前位置:
首页
JavaScript
JavaScript 参考手册
JavaScript 对象
源代码
清空
点击运行
<!DOCTYPE html> <html> <title>The CSSStyleDeclaration示例 - 基础教程(div.cn)</title> <body> <h1 style="color:red; font-size:3em;">CSSStyleDeclaration 对象</h1> <p>单击按钮返回元素的内联样式声明的值:</p> <button onclick="myFunc1()">点我试试</button> <p>单击按钮以返回元素的color属性的值:</p> <button onclick="myFunc2()">点我试试</button> <p id="result"></p> <script> var heading = document.getElementsByTagName("h1")[0]; var output = document.getElementById("result"); function myFunc1() { var styleObj = heading.style; output.innerHTML = styleObj.cssText; } function myFunc2() { var prop = window.getComputedStyle(heading, null).getPropertyValue("color"); output.innerHTML = prop; } </script> </body> </html>
运行结果