首页 > Web开发 > 详细

JS获得元素颜色

时间:2017-03-05 20:36:40      阅读:229      评论:0      收藏:0      [点我收藏+]
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body{background: RGB(222,222,2);}
</style>
</head>

<body>
<input type=‘button‘ value=‘点击按钮获取页面背景色‘ onclick="fun()"/>


<script type="text/javascript">
/*
*javascript的style属性只能获取内联样式,对于外部样式和嵌入式样式*需要用currentStyle属性。但这种方法有兼容性(FF和Chrome不支持)
*/
HTMLElement.prototype.__defineGetter__("currentStyle", function() {
return this.ownerDocument.defaultView.getComputedStyle(this, null);
});

function fun(){
var color = document.body.currentStyle.backgroundColor;
alert(color);
}
</script>
</body>

</html>

 

JS获得元素颜色

原文:http://www.cnblogs.com/cy2525/p/6506439.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!