源代码:
下载代码
点击运行
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <p>点击按钮计算 DIV 的背景颜色。</p> <p><button onclick="myFunction()">点我</button></p> <div id="test" style="height: 50px;background-color: rgb(178, 116, 230);">测试 DIV</div> <p>计算值为: <span id="demo"></span></p> <script> function myFunction(){ var elem = document.getElementById("test"); var theCSSprop = window.getComputedStyle(elem, null).getPropertyValue("background-color"); document.getElementById("demo").innerHTML = theCSSprop; } </script> </body> </html>
运行结果: